Python的文件处理与三元运算2-文件操作的其他操作方法

f=open(‘a.txt’,‘r+’,encoding=‘gb2312’)

data=f.read()

print(data)

f.write(‘你好’)

f=open(‘b.txt’,‘r+’,encoding=‘latin-1’)
data=f.read()
print(data)
f.write(‘aaaaaaaaaaa’)

f=open(‘b.txt’,‘r’,encoding=‘utf-8’,newline=’’) #读取文件中真正的换行符号
f=open(‘b.txt’,‘r+’,encoding=‘utf-8’,newline=’’) #读取文件中真正的换行符号

print(f.closed)
print(f.encoding)
f.flush()
print(f.readlines())

print(f.tell())#
f.readline()
print(f.tell())

f.seek(1)#控制光标移动
print(f.tell())
print(f.readlines())
f.seek(3)
print(f.tell())
print(f.read())

data=f.read(1)
print(data)

f.truncate(10)

f.flush() #讲文件内容从内存刷到硬盘

f.closed #文件如果关闭则返回True

f.encoding #查看使用open打开文件的编码

f.tell() #查看文件处理当前的光标位置

f.seek(3) #从开头开始算,将光标移动到第三个字节

f.truncate(10) #从开头开始算,将文件只保留从0-10个字节的内容,文件必须以写方式打开,但是w和w+除外

f=open(‘d.txt’,‘r’,newline=’’)

data=f.readline().encode(‘utf-8’)
print(data)
print(f.tell())

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())#----0
f.seek(10,1)
print(f.tell())#----10
f.seek(3,1)
print(f.tell())#----13

f=open(‘seek.txt’,‘rb’)
print(f.tell())
f.seek(-5,2)
print(f.read())
print(f.tell())
f.seek(3,1)
print(f.tell())

f=open(‘日志文件’,‘rb’)
data=f.readlines()
print(data[-1].decode(‘utf-8’))

f=open(‘日志文件’,‘rb’)

for i in f.readlines():
print(i)

循环文件的推荐方式
for i in f:
print(i)

for i in f:
offs=-10
while True:
f.seek(offs,2)
data=f.readlines()
if len(data) > 1:
print(‘文件的最后一行是%s’ %(data[-1].decode(‘utf-8’)))
break
offs*=2#加一倍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值