Python学习之路5——文件操作

Posted VisonWong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习之路5——文件操作相关的知识,希望对你有一定的参考价值。

 一、文件操作模式概述

1、打开文件的模式:

    • r, 只读模式【默认】
    • w,只写模式【不可读;不存在则创建;存在则删除内容;】
    • a, 追加模式【不可读;不存在则创建;存在则只追加内容;】

2、"+" 同时读写某个文件:

    • r+,可读写文件。【可读;可写;可追加】
    • w+,写读
    • a+,追加读

3、"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

    • rU
    • r+U

4、"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

    • rb
    • wb
    • ab

 

二、文件操作常用功能

  1、read()、readline()、readlines()的区别

  read()

1 #!/user/bin/env ptyhon
2 # -*- coding:utf-8 -*-
3 # Author: VisonWong
4 
5 info_file = open("yesterday", encoding="utf-8")  # 默认读取模式
6 print(info_file)  # 不加参数,直接打印
7 print(info_file.read())  # read参数,读取文件所有内容

  输出结果:

 1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
 2 <_io.TextIOWrapper name=yesterday mode=r encoding=utf-8>
 3 Oh, yesterday when I was young
 4 噢 昨日当我年少轻狂
 5 So many, many songs were waiting to be sung
 6 有那么那么多甜美的曲儿等我歌唱
 7 So many wild pleasures lay in store for me
 8 有那么多肆意的快乐等我享受
 9 And so much pain my eyes refused to see
10 还有那么多痛苦 我的双眼却视而不见
11 There are so many songs in me that wont be sung
12 我有太多歌曲永远不会被唱起
13 I feel the bitter taste of tears upon my tongue
14 我尝到了舌尖泪水的苦涩滋味
15 The time has come for me to pay for yesterday
16 终于到了付出代价的时间 为了昨日
17 When I was young
18 当我年少轻狂

  readine()

1 #!/user/bin/env ptyhon
2 # -*- coding:utf-8 -*-
3 # Author: VisonWong
4 
5 info_file = open("yesterday", encoding="utf-8")  # 默认读取模式
6 print(info_file.readline())  # readline,只读取文章中的一行内容

  输出结果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
2 Oh, yesterday when I was young
3 
4 
5 Process finished with exit code 0

  readlines()

1 #!/user/bin/env ptyhon
2 # -*- coding:utf-8 -*-
3 # Author: VisonWong
4 
5 info_file = open("yesterday", encoding="utf-8")  # 默认读取模式
6 
7 print(info_file.readlines())  # readlines,把文章内容以换行符分割,并生成list格式,数据量大的话不建议使用

  输出结果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
2 [Oh, yesterday when I was young\n, 噢 昨日当我年少轻狂\n, So many, many songs were waiting to be sung\n, 有那么那么多甜美的曲儿等我歌唱\n, So many wild pleasures lay in store for me\n, 有那么多肆意的快乐等我享受\n, And so much pain my eyes refused to see\n, 还有那么多痛苦 我的双眼却视而不见\n, "There are so many songs in me that won‘t be sung\n", 我有太多歌曲永远不会被唱起\n, I feel the bitter taste of tears upon my tongue\n, 我尝到了舌尖泪水的苦涩滋味\n, The time has come for me to pay for yesterday\n, 终于到了付出代价的时间 为了昨日\n, When I was young\n, 当我年少轻狂]
3 
4 Process finished with exit code 0

  2、seek,tell光标

  读取文件光标问题:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 #读取文件光标问题
 6 info_file = open("yesterday", encoding="utf-8")        # 文件句柄
 7 data = info_file.read()                                #默认光标在起始位置,.read()读取完后,光标停留到文件末尾
 8 data2 = info_file.read()                               #data2读取到的内容为空
 9 print(data)
10 print("--------",data2)
11 info_file.close()                                      #关闭文件

  输出结果:

 1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
 2 Oh, yesterday when I was young
 3 噢 昨日当我年少轻狂
 4 So many, many songs were waiting to be sung
 5 有那么那么多甜美的曲儿等我歌唱
 6 So many wild pleasures lay in store for me
 7 有那么多肆意的快乐等我享受
 8 And so much pain my eyes refused to see
 9 还有那么多痛苦 我的双眼却视而不见
10 There are so many songs in me that wont be sung
11 我有太多歌曲永远不会被唱起
12 I feel the bitter taste of tears upon my tongue
13 我尝到了舌尖泪水的苦涩滋味
14 The time has come for me to pay for yesterday
15 终于到了付出代价的时间 为了昨日
16 When I was young
17 当我年少轻狂
18 -------- 
19 
20 Process finished with exit code 0

  seek,tell方法

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 #用seek移动光标位置
 6 info_file = open("yesterday",encoding="utf-8")
 7 print(info_file.tell())                         #tell 获取当前的光标位
 8 print(info_file.readline().strip())
 9 print(info_file.readline().strip())
10 print(info_file.readline().strip())
11 print(info_file.tell())
12 info_file.seek(0)                               #seek 移动光标到文件首部
13 print(info_file.readline().strip())             #从文件首部开始打印
14 info_file.close()                               #关闭文件

  输出结果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
2 0
3 Oh, yesterday when I was young
4 噢 昨日当我年少轻狂
5 So many, many songs were waiting to be sung
6 107
7 Oh, yesterday when I was young
8 
9 Process finished with exit code 0

  3、文件循环

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 # 读取文件,并把第4行内容换成"-----我是分割线-------"
 6 
 7 info_file = open("yesterday", encoding="utf-8")
 8 
 9 for index, line in enumerate(info_file.readlines()):  # 先把文件内容以行为分割生成列表,数据量大不能用
10     if index == 3:
11         print("-----我是分割线-------")
12         continue
13     print(line.strip())
14 
15 
16 count = 0
17 for line in info_file:                                #建议使用方法,每读取一行,内存会把之前的空间清空,不会占用太多内存
18     count +=1
19     if count == 4:
20         print("-----我是分割线-------")
21         continue
22     print(line.strip())

  输出结果:

 1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
 2 Oh, yesterday when I was young
 3 噢 昨日当我年少轻狂
 4 So many, many songs were waiting to be sung
 5 -----我是分割线-------
 6 So many wild pleasures lay in store for me
 7 有那么多肆意的快乐等我享受
 8 And so much pain my eyes refused to see
 9 还有那么多痛苦 我的双眼却视而不见
10 There are so many songs in me that wont be sung
11 我有太多歌曲永远不会被唱起
12 I feel the bitter taste of tears upon my tongue
13 我尝到了舌尖泪水的苦涩滋味
14 The time has come for me to pay for yesterday
15 终于到了付出代价的时间 为了昨日
16 When I was young
17 当我年少轻狂
18 
19 Process finished with exit code 0

  4、flush刷新

1 # 模拟安装进度条
2 import sys, time  # 加载模块
3 
4 for i in range(40):
5     sys.stdout.write("#")
6     sys.stdout.flush()  # flush 强制刷新缓存到内存的数据写入硬盘
7     time.sleep(0.1)

   5、truncate截断

1 #!/user/bin/env ptyhon
2 # -*- coding:utf-8 -*-
3 # Author: VisonWong
4 
5 info_file = open("yesterday","a")       #非r、w模式
6 info_file.seek(10)
7 info_file.truncate(105)

  文件内容

1 Oh, yesterday when I was young
2 噢 昨日当我年少轻狂
3 So many, many songs were waiting to be sung

  6、with语句

  为了避免打开文件后忘记关闭,可以通过管理上下文,即

1 with open(log,r) as f:
2       
3     ...

  如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

  在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

1 with open(log1) as obj1, open(log2) as obj2:
2     pass 

  7、R+读写

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 #读写模式
 6 info_file = open("yesterday","r+",encoding="utf-8")       #读写模式
 7 print(info_file.readline().strip())
 8 print(info_file.readline().strip())
 9 print(info_file.tell())                             #查看读取两行后光标的位置
10 info_file.write("\nfffffffff")                    #没有写入数据到光标的位置,而是以追加的模式写到了文件最后
11 print(info_file.tell())                             #查看写入数据后光标的位置
12 print("----------\n",info_file.read())            #从上次读取的光标的位置开始读取到最后 注:新加入的内容不会打印
13 info_file.close()

  输出结果:

 1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
 2 Oh, yesterday when I was young
 3 噢 昨日当我年少轻狂
 4 62
 5 654
 6 ----------
 7  So many, many songs were waiting to be sung
 8 有那么那么多甜美的曲儿等我歌唱
 9 So many wild pleasures lay in store for me
10 有那么多肆意的快乐等我享受
11 And so much pain my eyes refused to see
12 还有那么多痛苦 我的双眼却视而不见
13 There are so many songs in me that wont be sung
14 我有太多歌曲永远不会被唱起
15 I feel the bitter taste of tears upon my tongue
16 我尝到了舌尖泪水的苦涩滋味
17 The time has come for me to pay for yesterday
18 终于到了付出代价的时间 为了昨日
19 When I was young
20 当我年少轻狂
21 
22 Process finished with exit code 0

  文件内容:  

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等我享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that wont be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂
17 fffffffff

  由上面的实例可知,读写模式下写入是追加写的,没有添加到指定行,而是写到文件的末尾。   r+模式下真的只是读和追加写吗?!看看下面的程序:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 #r+模式下对文件进行修改
 6 with open("yesterday","r+",encoding="utf-8") as info_file:
 7     file_read = info_file.read()
 8     info_file.seek(0)                                                #seek 光标移到文件首部
 9     new_file = file_read.replace("等我享受","等VisonWong享受")         #把文件进行修改
10     info_file.write(new_file)                                        #写入到文件中

  修改后文件内容:

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等VisonWong享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that wont be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂

  看完上面代码你可能会想,what ?

  第一个程序不是说光标跟文件的写入文件没关系吗?不应该会把修改的内容添加到文件末尾吗?

  怎么替换了?(黑人问号脸),来看看下面的程序:

1 #r+模式下对文件进行修改,文件修改在博客下面进行详细描述
2 with open("yesterday","r+",encoding="utf-8") as info_file:
3     file_read = info_file.read()
4     info_file.seek(0)                                                #seek 光标移到文件首部
5     print(info_file.readline())                                      #新增一行文件打印,光标到第一行未
6     new_file = file_read.replace("等我享受","等VisonWong享受")     #把文件进行修改
7     info_file.write(new_file)                                        #写入到文件中

  修改后文件内容:

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等我享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that wont be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂
17 Oh, yesterday when I was young
18 噢 昨日当我年少轻狂
19 So many, many songs were waiting to be sung
20 有那么那么多甜美的曲儿等我歌唱
21 So many wild pleasures lay in store for me
22 有那么多肆意的快乐等VisonWong享受
23 And so much pain my eyes refused to see
24 还有那么多痛苦 我的双眼却视而不见
25 There are so many songs in me that wont be sung
26 我有太多歌曲永远不会被唱起
27 I feel the bitter taste of tears upon my tongue
28 我尝到了舌尖泪水的苦涩滋味
29 The time has come for me to pay for yesterday
30 终于到了付出代价的时间 为了昨日
31 When I was young
32 当我年少轻狂

  这次在seek光标位置和对文件修改之间加了一条print,此刻会发现虽然光标在第一行尾末,但是新添加的内容写到了文件末尾,用的是追加模式。

  总结:

  r+模式下,如果在.write()进行写入内容前,有print()输出,则要写的内容会从文件尾部开始写入,使用的是读、追加模式;

  如果在.write()进行写入内容前,是seek()移动光标,则要写的内容会从移动到的光标开始进行写入,会把原来的内容覆盖掉,而不是整体后移,这点要记住;

  如果在.write()进行写入内容前,既没有print()也没有seek()光标移动,这种情况之前想的的情况,就是r+读写模式能先写后读吗?

  r+模式下默认光标在文件的首部,此时会直接从文件开头进行写入,效果等同于seek(0)。关于最后一点,参考a+模式。

  8、W+写读

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 # 写读模式
 6 info_file = open("yesterday2", "w+", encoding="utf-8")  # 写读模式 此模式一般不用
 7 
 8 info_file.write("噢 昨日当我年少轻狂\n")  # 向文件中写入四行内容
 9 info_file.write("有那么那么多甜美的曲儿等我歌唱\n")
10 info_file.write("有那么多肆意的快乐等我享受\n")
11 info_file.write("还有那么多痛苦 我的双眼却视而不见\n")
12 print(info_file.tell())  # 打印光标  此时光标在写入文件末尾
13 info_file.seek(0)  # 光标回到文件首部 如果不seek的话会从文件末尾打印,即为空
14 print(info_file.tell())
15 print(info_file.readline())  # 打印第一行,光标回到第一行末尾
16 info_file.write("------这一行应该写到第二行------")  # 理论上应该写在第一行的末尾后面
17 info_file.close()

  输出结果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
2 169
3 0
4 噢 昨日当我年少轻狂
5 
6 
7 Process finished with exit code 0

  修改后文件内容:

1 噢 昨日当我年少轻狂
2 有那么那么多甜美的曲儿等我歌唱
3 有那么多肆意的快乐等我享受
4 还有那么多痛苦 我的双眼却视而不见
5 ------这一行应该写到第二行------ 

  总结:

  读写模式一定要先写后读吗?能不能先读后写?  如果先读的话,由于用的是w+模式打开的文件,打开后会清空原文件内容,所有读取的到东西是空的。

  另W+模式后期用的很少,了解即可,包括a+追加读这种模式;

  另w+模式下,光标会跟随文件写入移到到文件末尾,不用seek移到光标的话,打印内容为空

  注:w+模式下,关于.write()跟seek()和print()的关系与r+模式下是一样一样的。w+打开文件后先清空,然后追加写,如果.write()前有seek()的话会从光标位置覆盖写

  9、a+追加读

1 #!/user/bin/env ptyhon
2 # -*- coding:utf-8 -*-
3 # Author: VisonWong
4 
5 #a+ 追加写
6 with open("yesterday","a+",encoding="utf-8") as info_file:          #追加写
7     print(info_file.tell())                                         #打印光标 默认在文件尾部
8     info_file.seek(0)                                               #seek 光标移到文件首部
9     info_file.write("----我是第一行------")                           #判断.write()与seek的关系

  输出结果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
2 645
3 
4 Process finished with exit code 0

  修改后文件内容:

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等我享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that wont be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂
17 ----我是第一行------

  总结:

  通过上面的程序可以得出,a+模式下光标位置为文件末尾,如果要print()的话要结合seek()进行使用;

  另外与r+、w+不同的是,.write()与seek()没有关系,只能写内容到文件末尾,一直都是追加模式!

 

  10、rb二进制读  

1 #!/user/bin/env ptyhon
2 # -*- coding:utf-8 -*-
3 # Author: VisonWong
4 
5 #二进制读取
6 info_file = open("yesterday","rb")          #二进制模式读取
7                                               #应用场景:网络传输
8 for i in info_file:
9      print(i)

  输出结果:

 1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/file_op.py
 2 bOh, yesterday when I was young\r\n
 3 b\xe5\x99\xa2 \xe6\x98\xa8\xe6\x97\xa5\xe5\xbd\x93\xe6\x88\x91\xe5\xb9\xb4\xe5\xb0\x91\xe8\xbd\xbb\xe7\x8b\x82\r\n
 4 bSo many, many songs were waiting to be sung\r\n
 5 b\xe6\x9c\x89\xe9\x82\xa3\xe4\xb9\x88\xe9\x82\xa3\xe4\xb9\x88\xe5\xa4\x9a\xe7\x94\x9c\xe7\xbe\x8e\xe7\x9a\x84\xe6\x9b\xb2\xe5\x84\xbf\xe7\xad\x89\xe6\x88\x91\xe6\xad\x8c\xe5\x94\xb1\r\n
 6 bSo many wild pleasures lay in store for me\r\n
 7 b\xe6\x9c\x89\xe9\x82\xa3\xe4\xb9\x88\xe5\xa4\x9a\xe8\x82\x86\xe6\x84\x8f\xe7\x9a\x84\xe5\xbf\xab\xe4\xb9\x90\xe7\xad\x89\xe6\x88\x91\xe4\xba\xab\xe5\x8f\x97\r\n
 8 bAnd so much pain my eyes refused to see\r\n
 9 b\xe8\xbf\x98\xe6\x9c\x89\xe9\x82\xa3\xe4\xb9\x88\xe5\xa4\x9a\xe7\x97\x9b\xe8\x8b\xa6 \xe6\x88\x91\xe7\x9a\x84\xe5\x8f\x8c\xe7\x9c\xbc\xe5\x8d\xb4\xe8\xa7\x86\xe8\x80\x8c\xe4\xb8\x8d\xe8\xa7\x81\r\n
10 b"There are so many songs in me that won‘t be sung\r\n"
11 b\xe6\x88\x91\xe6\x9c\x89\xe5\xa4\xaa\xe5\xa4\x9a\xe6\xad\x8c\xe6\x9b\xb2\xe6\xb0\xb8\xe8\xbf\x9c\xe4\xb8\x8d\xe4\xbc\x9a\xe8\xa2\xab\xe5\x94\xb1\xe8\xb5\xb7\r\n
12 bI feel the bitter taste of tears upon my tongue\r\n
13 b\xe6\x88\x91\xe5\xb0\x9d\xe5\x88\xb0\xe4\xba\x86\xe8\x88\x8c\xe5\xb0\x96\xe6\xb3\xaa\xe6\xb0\xb4\xe7\x9a\x84\xe8\x8b\xa6\xe6\xb6\xa9\xe6\xbb\x8b\xe5\x91\xb3\r\n
14 bThe time has come for me to pay for yesterday\r\n
15 b\xe7\xbb\x88\xe4\xba\x8e\xe5\x88\xb0\xe4\xba\x86\xe4\xbb\x98\xe5\x87\xba\xe4\xbb\xa3\xe4\xbb\xb7\xe7\x9a\x84\xe6\x97\xb6\xe9\x97\xb4 \xe4\xb8\xba\xe4\xba\x86\xe6\x98\xa8\xe6\x97\xa5\r\n
16 bWhen I was young\r\n
17 b\xe5\xbd\x93\xe6\x88\x91\xe5\xb9\xb4\xe5\xb0\x91\xe8\xbd\xbb\xe7\x8b\x82
18 
19 Process finished with exit code 0

  11、wb二进制写

1 #二进制写入
2 info_file = open("yesterday2","wb")          #二进制模式写入
3                                                  #应用场景与rb相似
4 info_file.write("噢 昨日当我年少轻狂\n".encode(utf-8))      #对写入的字符串进行编码
5 info_file.write("有那么那么多甜美的曲儿等我歌唱\n".encode(utf-8))
6 info_file.close()

  修改后文件内容:

1 噢 昨日当我年少轻狂
2 有那么那么多甜美的曲儿等我歌唱

  12、文件的修改 

  文件修改方式:

    • 把文件读取到内存当中,对内存进行修改,把修改后的内容写入到原文件(旧内容被清空)
    • 如果在硬盘上直接写,会进行覆盖,硬盘上不能进行插入,原来的内容不会整体后移,而是直接覆盖掉
    • 把文件读取到内存当中,对内存进行修改,把修改的内容另存为新的文件(旧文件保留)

  另存方式:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 info_file = open("yesterday", "r", encoding="utf-8")
 6 new_file = open("yesterday2", "w", encoding="utf-8")
 7 
 8 for line in info_file:
 9     if "等我享受" in line:
10         line = line.replace("等我享受", "等VisonWong享受")
11     new_file.write(line)

  修改后yesterday2内容:

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等VisonWong享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that wont be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂

  R+模式:

#r+模式下对文件进行修改,
with open("yesterday","r+",encoding="utf-8") as info_file:
    file_read = info_file.read()                               #加载内容到内存,此时光标在文件末尾
    new_file = file_read.replace("等我享受","等VisonWong享受")     #把文件进行修改
    info_file.truncate(0)                                      #清空原文件,不会影响光标位置
    info_file.seek(0)                                #移动光标到文件首部,不做操作的话,新的内容会添加到之前光标的位置
    info_file.write(new_file)                                        #修改的内容写入到文件中

  修改后yesterday内容:

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等VisonWong享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that wont be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂

  a+模式:

1 #a+模式下对文件进行修改,
2 with open("yesterday","a+",encoding="utf-8") as info_file:
3     info_file.seek(0)                                          #默认光标在文件末尾
4     file_read = info_file.read()                               #加载内容到内存,此时光标在文件末尾
5     new_file = file_read.replace("等我享受","等VisonWong享受")     #把文件进行修改
6     info_file.truncate(0)                                      #清空原文件,不会影响光标位置
7     info_file.seek(0)                                #移动光标到文件首部,不做操作的话,新的内容会添加到之前光标的位置
8     info_file.write(new_file)                                        #修改的内容写入到文件中

  修改后yesterday内容:

 1 Oh, yesterday when I was young
 2 噢 昨日当我年少轻狂
 3 So many, many songs were waiting to be sung
 4 有那么那么多甜美的曲儿等我歌唱
 5 So many wild pleasures lay in store for me
 6 有那么多肆意的快乐等VisonWong享受
 7 And so much pain my eyes refused to see
 8 还有那么多痛苦 我的双眼却视而不见
 9 There are so many songs in me that won‘t be sung
10 我有太多歌曲永远不会被唱起
11 I feel the bitter taste of tears upon my tongue
12 我尝到了舌尖泪水的苦涩滋味
13 The time has come for me to pay for yesterday
14 终于到了付出代价的时间 为了昨日
15 When I was young
16 当我年少轻狂

 

以上是关于Python学习之路5——文件操作的主要内容,如果未能解决你的问题,请参考以下文章

Python学习之路:文件操作之增删改查

Python学习之路5?文件处理

PYTHON学习之路_PYTHON基础

Python学习之路——模块

Python学习之路:基础知识之文件操作

开启python学习之路