文件操作-seek read tell

做~自己de王妃 提交于 2019-12-04 04:11:35
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())

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!