文件操作-seek read tell

f.seek(offset,whence)
offset: 相对偏移度 (光标移动的位数)针对的是字节
whence:指定光标位置从何开始
    0:从文件开头
    1:从当前位置
    2:从文件末尾
f.seek(
6, 0) f.seek(-3, 2)
简单的例子理解 seek read tell
with open('e','r+',encoding='utf-8') as f2:
f2.seek(3,0) #移动光标
print(f2.read(2)) #读几个 光标也会向后移动几个
print(f2.tell()) # 告诉位置

# with open(r"a.txt",'r')as f: # 打开文件的编码:gbk # # print(f.read(2)) # print(f.tell()) # f.seek(5,0) # print(f.read(2)) # print(f.read(5)) with open(r"a.txt", 'rt',encoding='utf-8')as f: # 打开文件的编码:gbk print(f.read()) f.seek(6, 0) print(f.tell()) print(f.read(2)) # with open(r"a.txt",'r')as f: # 打开文件的编码:gbk # # print(f.read(2)) # print(f.tell()) # f.seek(4,0) # fuck的卡仕达看但扩大 # print(f.read(2).encode('utf-8')) # with open(r'a.txt', 'rb')as f: # print(f.read(10).decode('gbk')) # f.seek(10, 1) # print(f.tell()) # with open(r'a.txt', 'rb')as f: # f.seek(10, 1) # print(f.tell()) # with open(r'a.txt','rb')as f: # f.seek(-3,2) # print(f.tell())

 

posted @ 2019-11-10 15:26  躺云飘  阅读(230)  评论(0编辑  收藏  举报