f = open("a.txt",'r')#-------tell 用法 print(f.read()) f = open("a.txt",'r') print(f.tell()) print(f.read(3)) print(f.tell()) f = open('a.txt', 'rb')#-------seek 用法 # 判断文件指针的位置 print(f.tell()) # 读取一个字节,文件指针自动后移1个数据 print(f.read(1)) print(f.tell()) # 将文件指针从文件开头,向后移动到 5 个字符的位置 f.seek(5) print(f.tell()) print(f.read(1)) # 将文件指针从当前位置,向后移动到 5 个字符的位置 f.seek(5, 1) print(f.tell()) print(f.read(1)) # 将文件指针从文件结尾,向前移动到距离 2 个字符的位置 f.seek(-1, 2) print(f.tell()) print(f.read(1))
输出:
C:\Python37\python3.exe "D:/autocore/code/python/python3Project/python3/函数用法/seek tell用法.py"
http://c.biancheng.net
0
htt
3
0
b'h'
1
5
b'/'
11
b'a'
21
b't'