补上:27天学习python内容文件处理其他方式

其他文件处理方式:

f=open("test2","r",newlin="")#
print(f.closed)
print(f.encoding)
print(f.tell())#光标在的位置
print(f.flush())#刷新,把修改的内存数据写到硬盘上
print(f.seek(1))#光标移动的位置,注意汉字可能报错
print(f.read(4))#read方法读的是4个字符
print(f.truncate(10))#从开头截取到10的位置,截取:表示是写文件。删除其他的只需要截取的


#怎样读取到最后一行日志
f=open("test2","rb")
#循环文件的方式建议使用这个
for i in f:
off=-10#定义一个偏移量,需要估测一行有多少个
while True:
f.seek(-off,2)#倒叙读
data=f.readline()
if len(data)>1:
print("文件最后一行%s" %(data[-1].decode()))
break
off*=2
原文地址:https://www.cnblogs.com/jianchixuexu/p/11566204.html