python基础-文件操作的其他方法

# f=open('code.txt','rb')#b的方式不能指定打开编码格式,以二进制的方式打开文件
# data=f.read()
# print(data)
# #encode 编码  decode解码
# print(data.decode(encoding="GBK"))
# f.close()
#



# f=open("test22.py",'wb')#b的方式不能指定编码,以二进制格式写入
# f.write(('111
').encode('GBK'))
# #f.write(bytes('1111111
',encoding='utf-8'))
#
#
# f=open("test22.py",'ab')#a 代表的是文件的最后一个位置
# f.write("唐".encode('GBK'))
# f.close()
#
# f=open("test22.py",'r',encoding='GBK')
# data=f.read()
# print(data)
# f=open("a.txt",'r',encoding='utf-8')
# print(f.closed)#查看是否关闭 返回True和False
# print(f.encoding)#文件打开的编码


#f=open("b.txt",'r+',encoding='utf-8',newline='')#读取文件中真正的换行符
# data=f.read()
# print(data)
# print(f.readline())
#
# #f.write("6666666
")#注意write形式已字符进行覆盖,默认也会把 
进行覆盖
# # f.flush()刷新缓存
# print(f.tell())显示光标当前位置


# f.seek(1)#除了read(3)以字符为单位,其余的seek,tell都已字节为单位
# print(f.tell())
# print(f.readline())


# data=f.read(4)
# print(data)


# f.truncate(2)#已字符为单位从头开始截取



# f=open("seek.txt",'r',encoding='utf-8')
# print(f.tell())
# f.seek(10)#默认从零开始计算光标
# print(f.tell())
# f.seek(3)
# print(f.tell())




# f=open("seek.txt",'rb')
# print(f.tell())
# f.seek(10)
# print(f.tell())
# f.seek(3,1)
# print(f.tell())




f=open("seek.txt",'rb')#f文件句柄
# print(f.tell())
# f.seek(-2,2)#倒序
# print(f.read())
# f.seek(3)
# print(f.tell())
# print(f)<_io.BufferedReader name='seek.txt'>

# for i in f:
#     print(i)



for i in f:
    offset=-3
    while True:
        f.seek(offset,2)#取出文件的最后一行
        data=f.readlines()
        if len(data)>1:
            print(data[-1])
            break
        offset*=2
如果我失败了,至少我尝试过,不会因为痛失机会而后悔
原文地址:https://www.cnblogs.com/tangcode/p/11010038.html