Python中的seek( ) 和 tell( )自制脑图 ,
介绍了二进制读文件、tell( )和seek( )。
读取 100 个字符,但读的是二进制,所以实际上是 100 个字节,但是像英文,一个字符就是一个字节,所以似乎没有什么区别。
tell ( ) 方法用来查看当前读取的位置
print('当前读取到了-->' , file_obj.tell( ) )
print(file_obj.read( 100 ) )
print(file_obj.read( 30 ) )就是读取到130。
seek( ) 可以修改当前读取的位置
file_obj.seek( )
先注释掉已读的:
with open('demo.txt' , 'rb') as file_obj:
print(file_obj.read( 100 ) )
print(file_obj.read( 30 ) )
file_obj.seek( )
print('当前读取到了-->' , file_obj.tell( ) )
注意:seek( ) 需要两个参数
第一个 是要切换到的位置
第二个 计算位置方式
可选值:
0 从头计算
1 从当前位置计算
2 从最后位置开始计算

