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

    01 今日内容大纲

    02 昨日内容回顾以及作业讲解

    1. 数据类型的补充

      • str:pass

      • tuple:

        • (1) ----> int ('alex') ----> str

        • count index

      • list:

        • sort sort(reverse= True) reverse()

        • 列表相加 列表与数字相乘:

        • 循环列表的问题

      • dict:

        • update 更新,增加值,修改值,创建字典,将一个字典的所有键值对覆盖添加到另一个字典。

        • dict.fromkeys(iterable,value) # 面试经常考

        • 循环字典的问题。

      • 数据类型的转换:0,{},[],set(),'',None

    2. 编码的进阶:

      ASCII, gbk,Unicode,utf-8 big5........

      1. 所有的编码本(除去Unicode之外)不能直接互相识别。

      2. 在内存中所有的数据必须是unicode编码存在,除去bytes。

        int

        bool

        tuple str bytes

        list

        dict

        set

         

         

     

    str bytes

    称呼: 文字文本 字节文本

    '' "" """ """ ''' ''' b'' b"" ........

    Unicode 非Unicode

     

     

    03 今日内容

    1. 文件操作的初识

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

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

        • 文件路径:path

        • 打开方式:读,写,追加,读写,写读......

        • 编码方式:utf-8,gbk ,gb2312......

      • 1  f1 = open('d:主妇空姐模特联系方式.txt',encoding='utf-8',mode='r')
        2  content = f1.read()
        3  print(content)
        4  f1.close()
      • 代码解释:

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

        • 1, 打开文件。

        • 2,对文件句柄进行相应操作。

        • 3,关闭文件。

      • 报错原因:

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

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

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

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

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

      for ***

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

       
       1 # read 全部读出来  **
       2  # f = open('文件的读', encoding='utf-8')
       3  # content = f.read()
       4  # print(content,type(content))
       5  # f.close()
       6  7  # read(n) 按照字符读取
       8  # f = open('文件的读', encoding='utf-8')
       9  # content = f.read(5)
      10  # print(content)
      11  # f.close()
      12 13  # readline()
      14  # f = open('文件的读', encoding='utf-8')
      15  # print(f.readline())
      16  # print(f.readline())
      17  # print(f.readline())
      18  # f.close()
      19 20  # readlines() 返回一个列表,列表中的每个元素是源文件的每一行。
      21  # f = open('文件的读', encoding='utf-8')
      22  # l1 = f.readlines()
      23  # for line in l1:
      24  #     print(line)
      25  # # print(l1)
      26  # f.close()
      27 28  # for 读取
      29  # f = open('文件的读', encoding='utf-8')
      30  # # ['abc太白金星最帅
      ', '老男孩最好的老师
      ', '老男孩教育是最好的学校
      ', 'fhsjdkfha
      ', 'fhdsfads
      ']
      31  # for line in f:
      32  #     print(line)
      33  # f.close()
      34 35  # f = open('美女.jpg',mode='rb')
      36  # content = f.read()
      37  # print(content)
      38  # f.close()
      39  
    3. 文件操作的写

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

       1  # 没有文件,创建文件,写入内容
       2  # f = open('文件的写',encoding='utf-8',mode='w')
       3  # f.write('随便写一点')
       4  # f.close()
       5  6  # 如果文件存在,先清空原文件内容,在写入新内容
       7  # f = open('文件的写',encoding='utf-8',mode='w')
       8  # f.write('太白最帅....')
       9  # f.close()
      10 11 12 13  # wb
      14  # f = open('美女.jpg',mode='rb')
      15  # content = f.read()
      16  # # print(content)
      17  # f.close()
      18  #
      19  # f1 = open('美女2.jpg',mode='wb')
      20  # f1.write(content)
      21  # f1.close()
      22

       

    4. 文件操作的追加

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

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

       
       1 # 读并追加  # 顺序不能错误。
       2  # f = open('文件的读写', encoding='utf-8', mode='r+')
       3  # content = f.read()
       4  # print(content)
       5  # f.write('人的一切痛苦,本质都是对自己无能的愤怒。')
       6  # f.close()
       7  8  # 错误示例:
       9  # f = open('文件的读写', encoding='utf-8', mode='r+')
      10  # f.write('人的一切痛苦,,,本质都是对自己无能的愤怒,,,')
      11  # content = f.read()
      12  # print(content)
      13  # f.close()

       

    6. 文件操作的其他功能

      总结:

      三个大方向:

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

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

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

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

       1  # tell 获取光标的位置 单位字节。
       2  # f = open('文件的读写', encoding='utf-8')
       3  # print(f.tell())
       4  # content = f.read()
       5  # # print(content)
       6  # print(f.tell())
       7  # f.close()
       8  9  # seek 调整光标的位置
      10  # f = open('文件的读写', encoding='utf-8')
      11  # f.seek(7)
      12  # content = f.read()
      13  # print(content)
      14  # f.close()
      15 16  # flush 强制刷新
      17  # f = open('文件的其他功能', encoding='utf-8',mode='w')
      18  # f.write('fdshdsjgsdlkjfdf')
      19  # f.flush()
      20  # f.close()

       

    7. 打开文件的另一种方式

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

      • 文件操作改的流程: 1, 以读的模式打开原文件。 2,以写的模式创建一个新文件。 3,将原文件的内容读出来修改成新内容,写入新文件。 4,将原文件删除。 5,将新文件重命名成原文件。

      • 具体代码:

       1 # low版
       2 # import os
       3 # # 1, 以读的模式打开原文件。
       4 # # 2,以写的模式创建一个新文件。
       5 # with open('alex自述',encoding='utf-8') as f1,
       6 #     open('alex自述.bak',encoding='utf-8',mode='w') as f2:
       7 # # 3,将原文件的内容读出来修改成新内容,写入新文件。
       8 #     old_content = f1.read()
       9 #     new_content = old_content.replace('alex', 'SB')
      10 #     f2.write(new_content)
      11 # os.remove('alex自述')
      12 # os.rename('alex自述.bak','alex自述')
      13 
      14 
      15 # 进阶版:
      16 import os
      17 # 1, 以读的模式打开原文件。
      18 # 2,以写的模式创建一个新文件。
      19 with open('alex自述',encoding='utf-8') as f1,
      20     open('alex自述.bak',encoding='utf-8',mode='w') as f2:
      21 # 3,将原文件的内容读出来修改成新内容,写入新文件。
      22     for line in f1:
      23         # 第一次循环 SB是老男孩python发起人,创建人。
      24         new_line = line.replace('SB', 'alex')
      25         f2.write(new_line)
      26 os.remove('alex自述')
      27 os.rename('alex自述.bak','alex自述')
      28 
      29 
      30 
      31 # 有关清空的问题:
      32 # 关闭文件句柄,再次以w模式打开此文件时,才会清空。
      33 # with open('文件的写', encoding='utf-8',mode='w') as f1:
      34 #     for i in range(9):
      35 #         f1.write('恢复贷款首付款')

    04 今日总结

    • 文件操作:

      • r w a rb wb r+ ab 重点记

      • read() write tell seek flush

      • 文件的改的代码必须会默写。

    05 预习内容

  • 相关阅读:
    CS 系统框架二[完善自动更新]
    CS 系统框架二
    CS 系统框架二[增加默认启动以及代码打开窗体]
    2022届宝鸡质检[1]文数参考答案
    2022届宝鸡质检[1]理数参考答案
    合并DataTable并排除重复数据的通用方法
    IE6鼠标闪烁之谜
    Windows下MemCache多端口安装配置
    XML解析文件出错解决方法
    巧用row_number和partition by分组取top数据
  • 原文地址:https://www.cnblogs.com/zhangxiangning/p/10212242.html
Copyright © 2011-2022 走看看