zoukankan      html  css  js  c++  java
  • Python Third Day-文件处理

    文件处理

    1 打开文件,得到文件句柄并赋值给一个变量
    f=open('a.txt','r',encoding='utf-8')#默认打开的方式为r指的是文本文件,全名为‘rt’#w文件方式指的是如果有a.txt就覆盖,
    没有a.txt文件就创建,如果是print(f.weitable()),指的是判断是可写的,如果是w就是True,r就是False
    1 格式w
    2 f=open('b.txt','w',encoding='utf-8')
    3 print(f.writable())
    4 f.close
    写入文件文件的事例
     1 格式一
     2 f=open('b.txt','w',encoding='utf-8')
     3 f.write('1111
    345345345')
     4 f.close
     5 格式二
     6 f=open('b.txt','w',encoding='utf-8')
     7 f.write('1111
    ')
     8 f.write('678678678
    ')
     9 f.close
    10 格式三
    11 f=open('b.txt','w',encoding='utf-8')
    12 f.writelines(['3333
    555555'])#以元组或者列表的形式写入
    13 f.close
    14 格式四
    15 w写的时候指的是从文件的开头写,文件不存在创建
    16 f=open('b.txt','w',encoding='utf-8')#
    17 f.write('456456
    ')
    18 f.close
    19 a:文件不存在则创建,文件存在那么在打开文件后立刻将光标移动到文件末尾,进行追加写
    20 f=open('b.txt','a',encoding='utf-8')
    21 f.write('567567
    ')
    22 f.close
    
    
    #r:读
    # f=open(r'b.txt','r',encoding='utf-8')
    # # print(f.read())#把文件内容都读到内存中,只限有文件小的文件
    # # print(f.readlines())#指的是读完放到列表中
    # print(f.readline(),end='')#这个指的是一条条的读
    # print(f.readline(),end='')
    # f.close()
    一行行的读,可以用循环,while或for
    with open('b.txt','r',encoding='utf-8') as f:#w写的时候指的是从文件的开头写
        while True:
            line=f.readline()
            if len(line)==0:break
            print(line)
    用for#从文件中循环取出多个值
    for line in f:
      print(line)
    
    
    1 #b:bytes(二进制),rb可以读文本文件,也可以读图片(建议用这个)
    2 with open('b.txt', 'rb') as f:#文本文件可以变回输出的文件中的内容用decode('utf-8'3     print(f.read().decode('utf-8'))
    4 with open('微信图像.jpg', 'rb') as f:
    5     print(f.read())

    #with open('b.txt', 'wb') as f:
      f.write('hello world'.encode('utf-8'))

    # with open('b.txt','ab') as f:
    # res='哈哈哈'.encode('utf-8')
    # print(res,type(res))
    # f.write(res)

     1 cp命令
     2 源文件大小
     3 文件打开模式问题
     4 import sys#倒入模块
     5 #print(sys.argv)#接收脚本执行时给脚本传的参数,指的是吧py文件拿到命令行执行,py文件后面写上参数
     6 _,src_file,dst_file=sys.argv
     7 with open(src_file,'rb') as read_f,
     8         open(dst_file,'wb') as write_f:
     9     # data=read_f.read()
    10     # write_f.write(data)#源文件大容易被干死
    11      for line in read_f:#一行行的读
    12         write_f.write(line)
    13         #write_f.flush()这个意思是写一行赶紧给我刷到硬盘中去,效率低
    1 对sys_argv的解释
    2 import sys
    3 print(sys.argv)
    截图中输入的src_file,dst_file是给脚本的赋值,都传到argv中


    import sys print(sys.argv)
    l=['F:\python20期\02练习.py', 'src_file', 'dst_file']#定义的l就相当于argv
    # sfile=l[1]
    # dfile=l[2]提取l[1]和l[2]可以用另一种方式取出_,sfile,dfile用这两种方式取
    _,sfile,dfile=l#l相当于sys.argv
    #所以直接可以写成
    import sys#倒入模块
    print(sys.argv)
    _,sfile,dfile=sys.argv

    # import os
    # with open('access.log','r',encoding='utf-8') as read_f
    # ,open('.access.log.swap','w',encoding='utf-8')as write_f:#swap坐下中转
    # data=read_f.read()
    # data=data.replace('zhongguo','hello')#只是在内存中修改的
    # write_f.write()
    #
    # os.remove('access.log')#
    # os.rename('.access.log.swap','b.txt')#把b.txt.swap重名名为b.txt
    上面的这种方式如果文件过大,内存容易爆
    import so
    # with open('access.log','r',encoding='utf-8') as read_f
    # ,open('.access.log.swap','w',encoding='utf-8')as write_f:
          for line in read_f:#一行行读出
            if 'alex' in line#如果alex在line就修改
              line=line.replace('alex','SB')#这个还没有替换line.replace('alex','SB'),要重新赋值
            write_f.write(line)
      os.remove('
    access.log')
      os.rename('.access.log.swap','access.log')

     
    文件内光标移动
     1 #只有一种情况光标以字符为单位:文件以rt方式打开,read(3)c.txt为hello你
     2 # with open('c.txt','rt',encoding='utf-8') as f:
     3 #     # print(f.read(6))
     4 #     # print(f.tell())
     5 #     # f.seek(0,0)
     6 #     # print(f.read(6))
     7 #   f.seek(0,0)#光标移动的字节,第二个是指第几个字节,第二个0代报参照物(只有0在rt模式下使用),代表相对的位置在哪,还可以是1,2,这里的0代表回到首位
     8    证明 
     9 #     # f.seek(6,0)
    10 #     f.seek(8,0)
    11 #     print(f.read())

     截断文件

    1 with open('sa.txt','a',encoding='utf-8') as f:
    2     f.truncate(3)#指定大小

    如果是windows系统的时候
    f=open(r'c:\a.txt','r',encoding='utf-8')第一个r指的是取消掉右斜杠的转意,如果要是当前文件就直接写文件名就好
    2 通过句柄文件进行操作
    data=f.read()
    3 关闭文件
    f.close

    f=open('a.txt','r')的过程分析

    #1、由应用程序向操作系统发起系统调用open(...)
    
    #2、操作系统打开该文件,并返回一个文件句柄给应用程序
    
    #3、应用程序将文件句柄赋值给变量f
  • 相关阅读:
    人工智能,人工神经网络,机器学习,深度学习,卷积神经网络 时间线与内在联系(转载)
    基于spark logicplan的表血缘关系解析实现
    spark ml pipeline构建机器学习任务
    常用特征离散化方法
    spark sql插入表时的文件个数研究
    Spark累加器(Accumulator)
    java中使用URLClassLoader访问外部jar包的java类
    Scala里面的排序函数的使用
    Spark获取DataFrame中列的几种姿势--col,$,column,apply
    spark.sql.shuffle.partitions和spark.default.parallelism的区别
  • 原文地址:https://www.cnblogs.com/yinfutao/p/8053014.html
Copyright © 2011-2022 走看看