移动光标(每次读取信息,可以理解为有一个光标在移动,读完信息,光标移动到最后)
1.读取光标:
1 f = open("file",'r',encoding = 'utf-8') 2 print(f.tell())
返回0
2.tell 读取信息是按字符个数
1 f = open("file",'r',encoding = 'utf-8') 2 3 f.readline() 4 print(f.tell()) # 结果为23
3.tell读取光标位置,以及read的使用
1 f = open("file2",'r',encoding = 'utf-8') 2 f.readline() 3 print(f.tell()) 4 5 f.read(5) 6 print(f.tell()) 7 8 f.read() 9 10 print(f.tell()) 11 12 '''结果 13 23 14 28 15 115 16 '''
4.seek指定移动光标位置,可以与tell 搭配使用
文件file2:
This is a hello world
This is a hello world
This is a hello world
This is a hello world
This is a hello world
1 f = open("file2",'r',encoding = 'utf-8') 2 f.readline() 3 print(f.tell()) 4 5 f.read(5) 6 print(f.tell()) 7 8 f.seek(0) 9 print(f.tell()) 10 11 f.seek(10) 12 13 print(f.readline()) 14 print(f.tell()) 15 '''结果 16 23 17 28 18 0 19 hello world 20 21 23 22 '''
5.进度条,利用flush 强制刷新。 f.flush() 强制刷新 ,不使用flush 时 ,等缓冲区满了才会将数据刷到硬盘中
1 import time 2 import sys 3 4 def jindu(): 5 for i in range(20): 6 sys.stdout.write("#") 7 time.sleep(0.1) 8 sys.stdout.flush() 9 10 jindu()
6.其他
f = open("file2",'a',encoding = 'utf-8') # print(f.fileno()) 返回一个编号,操作系统io接口的编号 6 # seekable()判断是否可以移动光标 # readable() writeable() 判断文件是否可读可写 ,返回 True False # f.flush() 强制刷新 ,不使用flush 时 ,等缓冲区满了才会将数据刷到硬盘中 f.truncate(20) # 从头开始截取 20 个字符 结果:This is a hello worl
7.文件读写 r+(在最后添加)
f = open("file2",'r+',encoding = 'utf-8') # 读写 print(f.readline()) print(f.readline()) print(f.readline()) f.write("hhhhh") # 不管光标位置,直接在最后添加 f.close()
f = open("file2",'r+',encoding = 'utf-8') # 读写 f.seek(5) f.write("qqqq") f.close() ''' helloqqqqld! hello world! hello world! hello world! hello world! '''
8.文件写读 w+ (先创建一个文件,再写入)
1 f = open("file2",'w+',encoding = 'utf-8') # 读写 2 f.write("-------kry------ ") 3 f.write("-------kry------ ") 4 f.write("-------kry------ ") 5 print(f.tell()) 6 f.write("Q") 7 f.seek(10) 8 print(f.tell()) 9 f.write("hello") 10 print(f.tell()) 11 f.write("!") 12 f.close() # 结果可以再字符10的位置覆盖 13 14 ''' 15 -------kryhello! 16 -------kry------ 17 -------kry------ 18 Q 19 '''
追加读 a+
9.
rb 二进制读
wb 二进制写
f = open("file2",'rb') # 读写 print(f.readline()) f.close()s ''' b'helloqqqqld! ' ''' #网络传输只能用二进制文件
10.文件修改 (使用rb ,wb 修改文件会把原位置的内容修改掉,为了避免这种情况,一是把文件先放在内存中,可以避免把原内容修改掉。二打开一个文件,修改后写在另一个文件里)
f = open("file","r",encoding = "utf-8") f_new = open("file3","w",encoding = "utf-8") for line in f: if "时间" in line: line = line.replace("时间","我的时间") f_new.write(line) f.close() f_new.close()
11.实现shell sed 替换功能(通过脚本改变参数)
1 import sys 2 3 find_str = sys.argv[1] 4 replace_str = sys.argv[2] 5 6 f = open("file","r",encoding = "utf-8") 7 f_new = open("file3","w",encoding = "utf-8") 8 9 for line in f: 10 if find_str in line: 11 line = line.replace(find_str,replace_str) 12 f_new.write(line) 13 f.close() 14 f_new.close()
12. with 语句 (为了避免打开文件忘记关闭,可以用with 管理上下文)
# 打开一个文件 with open("file","r",encoding = "utf-8") as f: for line in f: print(line) # 打开多个 with open("file","r",encoding = "utf-8") as f, open("file1","r",encoding = "utf-8") as f2: for line in f: print(line) for line in f2: print(line)