python文件的定位读写

1.获取当前读写的位置。在读写的过程中,如果想知道当前的位置,可以使用tell()来获取
 1 #打开一个已经存在的文件
 2 f = open('test.txt','r')
 3 str = f.read(3)
 4 print("读取的数据是:",str)
 5 #查找当前位置
 6 position = f.tell()
 7 print("当前位置:",position)
 8 
 9 str = f.read(6)
10 print("读取的数据是:",str)
11 # 查找当前位置
12 position = f.tell()
13 print("当前文件位置 : ", position)
14 #关闭文件
15 f.close()

原文地址:https://www.cnblogs.com/ma1998/p/12262745.html