zoukankan      html  css  js  c++  java
  • 文件操作的初识

    文件操作的初识

    1. 文件操作的初识

      • 护士空姐少妇的联系方式.txt

      • 利用python代码写一个很low的软件,去操作文件。

        • 文件路径:path
        • 打开方式:读,写,追加,读写,写读......
        • 编码方式:utf-8,gbk ,gb2312......
      • f1 = open('d:主妇空姐模特联系方式.txt',encoding='utf-8',mode='r')
        content = f1.read()
        print(content)
        f1.close()
        
      • 代码解释:

        open 内置函数,open底层调用的是操作系统的接口。
        f1,变量,f1,fh,file_handler,f_h,文件句柄。 对文件进行的任何操作,都得通过文件句柄. 的方式。
        encoding:可以不写,不写参数,默认编码本:操作系统的默认的编码
        windows: gbk。
        linux: utf-8.
        mac : utf-8.
        f1.close() 关闭文件句柄。
        
      • 文件操作的三部曲:

        • 1, 打开文件。
        • 2,对文件句柄进行相应操作。
        • 3,关闭文件。
      • 报错原因:

        • UnicodeDecodeError:文件存储时与文件打开时编码本运用不一致。

        • 第二个错误: 路径分隔符产生的问题:

          • r'C:UsersoldboyDesktop主妇空姐模特联系方式.txt'
            
    2. 文件操作的读

      r , rb, r+,r+b 四种模式

      ​ r: read()** read(n) readline() readlines()

      ​ for ***

      ​ rb: 操作的是非文本的文件。图片,视频,音频。

      # read 全部读出来  **
      # f = open('文件的读', encoding='utf-8')
      # content = f.read()
      # print(content,type(content))
      # f.close()
      
      # read(n) 按照字符读取
      # f = open('文件的读', encoding='utf-8')
      # content = f.read(5)
      # print(content)
      # f.close()
      
      # readline()
      # f = open('文件的读', encoding='utf-8')
      # print(f.readline())
      # print(f.readline())
      # print(f.readline())
      # f.close()
      
      # readlines() 返回一个列表,列表中的每个元素是源文件的每一行。
      # f = open('文件的读', encoding='utf-8')
      # l1 = f.readlines()
      # for line in l1:
      #     print(line)
      # # print(l1)
      # f.close()
      
      # for 读取
      # f = open('文件的读', encoding='utf-8')
      # # ['abc太白金星最帅
      ', '老男孩最好的老师
      ', '老男孩教育是最好的学校
      ', 'fhsjdkfha
      ', 'fhdsfads
      ']
      # for line in f:
      #     print(line)
      # f.close()
      
      # f = open('美女.jpg',mode='rb')
      # content = f.read()
      # print(content)
      # f.close()
      
    3. 文件操作的写

      w,wb, w+,w+b 四种模式

      # 没有文件,创建文件,写入内容
      # f = open('文件的写',encoding='utf-8',mode='w')
      # f.write('随便写一点')
      # f.close()
      
      # 如果文件存在,先清空原文件内容,在写入新内容
      # f = open('文件的写',encoding='utf-8',mode='w')
      # f.write('太白最帅....')
      # f.close()
      
      
      
      # wb
      # f = open('美女.jpg',mode='rb')
      # content = f.read()
      # # print(content)
      # f.close()
      #
      # f1 = open('美女2.jpg',mode='wb')
      # f1.write(content)
      # f1.close()
      
      
    4. 文件操作的追加

      a, ab, a+,a+b 四种模式

      # 没有文件创建文件,追加内容
      # f = open('文件的追加',encoding='utf-8',mode='a')
      # f.write('太白最帅....')
      # f.close()
      
      # 有文件,在原文件的最后面追加内容。
      # f = open('文件的追加',encoding='utf-8',mode='a')
      # f.write('大壮,舒淇,b哥,雪飞')
      # f.close()
      
      
    5. 文件操作的其他模式 r+

      # 读并追加  # 顺序不能错误。
      # f = open('文件的读写', encoding='utf-8', mode='r+')
      # content = f.read()
      # print(content)
      # f.write('人的一切痛苦,本质都是对自己无能的愤怒。')
      # f.close()
      
      # 错误示例:
      # f = open('文件的读写', encoding='utf-8', mode='r+')
      # f.write('人的一切痛苦,,,本质都是对自己无能的愤怒,,,')
      # content = f.read()
      # print(content)
      # f.close()
      
    6. 文件操作的其他功能

      总结:

      ​ 三个大方向:

      读,四种模式: r rb r+ r+b

      写,四种模式 : w,wb, w+,w+b

      追加 四种模式: a, ab, a+,a+b

      相应的功能:对文件句柄的操作:read read(n) readline() readlines() write()

      # tell 获取光标的位置 单位字节。
      # f = open('文件的读写', encoding='utf-8')
      # print(f.tell())
      # content = f.read()
      # # print(content)
      # print(f.tell())
      # f.close()
      
      # seek 调整光标的位置
      # f = open('文件的读写', encoding='utf-8')
      # f.seek(7)
      # content = f.read()
      # print(content)
      # f.close()
      
      # flush 强制刷新
      # f = open('文件的其他功能', encoding='utf-8',mode='w')
      # f.write('fdshdsjgsdlkjfdf')
      # f.flush()
      # f.close()
      
    7. 打开文件的另一种方式

      # 优点1: 不用手动关闭文件句柄
      # with open('文件的读',encoding='utf-8') as f1:
      #     print(f1.read())
      
      # 优点2:
      with open('文件的读', encoding='utf-8') as f1,
              open('文件的写', encoding='utf-8', mode='w')as f2:
          print(f1.read())
          f2.write('hfdsjkghkajhsdjg')
      
      # 缺点:待续。
      
    8. 文件操作的改

      • 文件操作改的流程:
        1, 以读的模式打开原文件。
        2,以写的模式创建一个新文件。
        3,将原文件的内容读出来修改成新内容,写入新文件。
        4,将原文件删除。
        5,将新文件重命名成原文件。
      • 具体代码:
      # low版
      # import os
      # # 1, 以读的模式打开原文件。
      # # 2,以写的模式创建一个新文件。
      # with open('alex自述',encoding='utf-8') as f1,
      #     open('alex自述.bak',encoding='utf-8',mode='w') as f2:
      # # 3,将原文件的内容读出来修改成新内容,写入新文件。
      #     old_content = f1.read()
      #     new_content = old_content.replace('alex', 'SB')
      #     f2.write(new_content)
      # os.remove('alex自述')
      # os.rename('alex自述.bak','alex自述')
      
      
      # 进阶版:
      import os
      # 1, 以读的模式打开原文件。
      # 2,以写的模式创建一个新文件。
      with open('alex自述',encoding='utf-8') as f1,
          open('alex自述.bak',encoding='utf-8',mode='w') as f2:
      # 3,将原文件的内容读出来修改成新内容,写入新文件。
          for line in f1:
              # 第一次循环 SB是老男孩python发起人,创建人。
              new_line = line.replace('SB', 'alex')
              f2.write(new_line)
      os.remove('alex自述')
      os.rename('alex自述.bak','alex自述')
      
      
      
      # 有关清空的问题:
      # 关闭文件句柄,再次以w模式打开此文件时,才会清空。
      # with open('文件的写', encoding='utf-8',mode='w') as f1:
      #     for i in range(9):
      #         f1.write('恢复贷款首付款')
      

    04 今日总结

    • 文件操作:
      • r w a rb wb r+ ab 重点记
      • read() write tell seek flush
      • 文件的改的代码必须会默写。
  • 相关阅读:
    TensorFlow---基础---GFile
    TensorFlow---image recognition--classify_image运行、文件说明与错误(路径)解决
    gcc -D
    c语言数据读入---sscanf、fscanf
    Mysql--视图
    Mysql的索引
    每天学点linux命令之nc
    Redis基础---链接管理
    iOS WKWebview 网页开发适配指南
    Win7/Win10下搭建Go语言开发环境
  • 原文地址:https://www.cnblogs.com/wyh0717/p/12911188.html
Copyright © 2011-2022 走看看