# f.seek
# 文件内指针移动,只有t模式下的read(n),n代表的字符的个数
# 除此以外文件内指针的移动都是以字节为单位
# with open('a.txt',mode='rt',encoding='utf-8') as f:
# msg=f.read(1)
# print(msg)
# with open('a.txt',mode='rb') as f:
# msg=f.read(3)
# print(msg.decode('utf-8'))
# f.seek(offset,whence)有两个参数:
# offset: 代表控制指针移动的字节数
# whence: 代表参照什么位置进行移动
# whence = 0: 参照文件开头(默认的),特殊???,可以在t和b模式下使用
# whence = 1: 参照当前所在的位置,必须在b模式下用
# whence = 2: 参照文件末尾,必须在b模式下用
# with open('a.txt',mode='rt',encoding='utf-8') as f:
# f.seek(6,0)
# msg=f.read(1)
# print(msg)
# with open('a.txt',mode='rb') as f:
# f.seek(3,0)
# msg=f.read(3)
# print(msg.decode('utf-8'))
# with open('a.txt',mode='rb') as f:
# msg=f.read(3)
# # print(msg.decode('utf-8'))
# print(f.tell())
# # f.seek(6,0)
# f.seek(3,1)
# msg1=f.read(3)
# print(msg1.decode('utf-8'))
# with open('a.txt',mode='rb') as f:
# msg=f.read(3)
# # print(msg.decode('utf-8'))
# print(f.tell())
# # f.seek(6,0)
# f.seek(3,1)
# msg1=f.read(3)
# print(msg1.decode('utf-8'))
# with open('a.txt',mode='rb') as f:
# # f.seek(0,2)
# # print(f.tell())
# f.seek(-3,2)
# msg=f.read(3)
# print(msg.decode('utf-8'))
# with open('access.log',mode='rb') as f:
# f.seek(0,2) # 当前位置是147bytes
#
# while True:
# line=f.readline() # 当前位置是196bytes
# # print(f.tell())
# if len(line) == 0:
# # 没有新的一行内容追加进来
# pass
# else:
# # 有新的一行内容追加进来
# print(line.decode('utf-8'),end='')
# with open('access.log',mode='rb') as f:
# f.seek(0,2) # 当前位置是147bytes
#
# while True:
# line=f.readline() # 当前位置是196bytes
# if len(line) != 0:
# print(line.decode('utf-8'),end='')
with open('a.txt',mode='r+t',encoding='utf-8') as f:
f.truncate(6)