#-*- coding = utf-8 -*-
#@Time:2020/8/6 13:32
#@Author:huxuehao
#@File:049_文件_seek()和tell().py
#@Software:PyCharm
#@Emial:
#@Theme:
# 一、tell() 用来查看当前读取到的位置
# 二、seek() 用来修改当前读取的位置,以便控制下一次读取的起始位置
# -seek( 要切换到的位置 , 计算位置方式【默认】) 需要两个参数
# 第一个:要切换到的位置
# 第二个:计算位置方式
# 可选值:
# 0 :从头开始计算(默认)
# 1 :从当前位置
# 2 :从尾位置开始计算
# 注意:读文本文件时,如果是utf-8,1个中文占3个字节
filename='demo.txt'
# 以二进制的形式来的demo.txt
with open(filename,'rb') as file_obj:
print(file_obj.read(20))
print('当前读取到 -->',file_obj.tell(),end='\n\n') #用来查看当前读取到的位置
print("通过seek() 方法改变读取倒的位置,以便控制下一次读取的起始位置")
file_obj.seek(0)
print('当前读取到 -->', file_obj.tell())
print(file_obj.read(20))
049_文件_seek()和tell().py
最新推荐文章于 2024-04-24 13:35:29 发布