博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python--005--文件操作(b,其他)
阅读量:4678 次
发布时间:2019-06-09

本文共 3230 字,大约阅读时间需要 10 分钟。

1. 文件操作b模式
什么情况下使用b模式:   1)默认r w 是rt wt即默认为文本方式,如果是其他形式如视频等,则可使用b模式   2)b模式可以跨平台   3)对linux平台无用,因为linux平台就是以二进制来处理的,对windows系统有用
#  ===========rb模式 (读)============ 注:1)b 的方法不能指定编码
2) 字符串-----encode----->byte     bytes -----decode ----->字符串
# f = open('test11.py','rb',encoding='utf-8')  # b的方法不能指定编码f = open('test11.py','rb')   # r 表示读data = f.read()print(data)  # 输入为b'hello1\r\n2222\r\n3333\r\n4444\r\n\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a\xe4\xbd\xa9\xe5\xa5\x87'data1=data.decode('utf-8')print(data1)f.close()
output:b'hello1\r\n2222\r\n3333\r\n4444\r\n\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a\xe4\xbd\xa9\xe5\xa5\x87'hello1222233334444你好啊佩奇
#  ===========wb模式 (写)============
f = open('test33.py','wb')f.write(bytes('11你好啊11\n',encoding='utf-8'))f.write('11你好11\n'.encode('utf-8'))output:test33.py:11你好啊1111你好11
#  ===========ab模式 (写到最后一个位置)============
f = open('test33.py','ab')f.write('11你好11\n'.encode('utf-8'))output:test33.py:11你好啊1111你好1111你好11
 
2. 其他方法
f.closed   # 判断文件是否关闭,True或False
f.encoding   # encoding 表示文件打开的编码
f = open('a.txt','r',encoding='utf-8')# f = open('a.txt','r',encoding='latin-1')print(f.closed)     # 判断文件是否关闭,True或Falseprint(f.encoding)    # encoding 表示文件打开的编码f.close()output:Falseutf-8
f.flush()  # 在内存中,执行过flush后才会刷到,保存到硬盘中
f.tell()  # tell,告诉光标所在位置
f = open('a.txt','r',encoding='utf-8')print(f.tell())    # tell,告诉光标所在位置print(f.readlines())print(f.tell())    # tell,告诉光标所在位置f.close()output:0['你好\n', 'dd']10

 

# 文件内光标移动 注意:read(3)代表读取3个字符,其余的文件内光标都是以字节为单位如seek,read ,tell,truncate
f = open('a.txt','rb')print(f.read())    #换行是\r\noutput:b'\xe4\xbd\xa0\xe5\xa5\xbd\r\ndd'

 

f = open('a.txt','r',encoding='utf-8',newline='')data = f.read(1)    # 读的单位是字符,会读出 你,而seek读1个会报错,因为读的是字节print(data)output:你

 

f.readlines()  #一行一行的读,读出来返回一个列表,其中包括换行
f = open('a.txt','r',encoding='utf-8')print(f.readlines())output:['你好\n', 'dd']
f = open('a.txt','r',encoding='utf-8',newline='')print(f.readlines())output:['你好\r\n', 'dd']
注:因为不指定newline,python会自动进行处理将换行统一换成\n,指定newline为空,就不处理,windows平台下就为\r\n
f = open('a.txt','r',encoding='utf-8',newline='')print(f.tell())    # tell,告诉光标所在位置f.readline()print(f.tell())    # tell,告诉光标所在位置output:08    # 注:换行为\r\n ,所以 “你好”(6个)+换行(\r\n) =8 
f.seek()    # 指定光标位置

 

f = open('a.txt','r',encoding='utf-8')f.seek(0)    # 指定光标位置print(f.tell())f.seek(3)    # 指定光标位置print(f.tell())output:03
 

 

f.truncate(n)  #  截取n个,truncate不能是用w和w+打开,因为w会覆盖写
f = open('a.txt','r+',encoding='utf-8',newline='')   # truncate不能是用w和w+打开f.truncate(9)  #  截取9个 a.txt:你好 d
 
f.seek(3)   #都以0的位置开始,即文件开头开始
f.seek(3,1) # 还可以 以1的位置开始,即文件相对位置 ,注意:单位是字节,所以必须以rb的方式打开

 

f = open('seek.txt','r',encoding='utf-8')print(f.tell())f.seek(10)  # f.seek(10,0)print(f.tell())f.seek(3)  # f.seek(10,0)print(f.tell()) output:   都以0的位置开始,即文件开头开始 0 10 3seek.txthello你好123123
f = open('seek.txt','rb')f.seek(3,1)  #以1的位置开始,即文件相对位置 ,注意:单位是字节,所以必须以rb的方式打开print(f.tell())f.seek(10,1)  #print(f.tell())output:313

 

f.seek(-5, 2) # 2表示倒叙,从文件末尾开始读,前面得加一个负号  
主要应用:日志文件很大,查找日志文件中最后的内容
f = open('seek.txt','rb')f.seek(-5, 2) # 2表示倒叙,从文件末尾开始读,前面得加一个负号print(f.read())print(f.tell())output:b'\r\n123'23

 

注:

循环文件的推荐方式,文件很大的情况下,不推荐readlines  f = open('seek.txt','rb')  for i in f:      print(i)

   

转载于:https://www.cnblogs.com/jinf/p/10928785.html

你可能感兴趣的文章
js中用变量作为$()内id的值、动态获取id,及获取其下面的class元素
查看>>
NSXMLParser详解(事例)
查看>>
[BZOJ 3680]吊打XXX(模拟退火)
查看>>
转:adb操作命令详解及大全
查看>>
Tomcat端口被占用解决方法
查看>>
Cocoapods的安装笔记
查看>>
【转载】九宫格抽奖转盘实例
查看>>
c++ 在控制台用 wcout输出宽字符的问题
查看>>
java使字符串的数字加一
查看>>
【转】C#三大特性之 封装、继承、多态
查看>>
字符数组拷贝2
查看>>
chVsprintf
查看>>
输入和输出
查看>>
Spark记录-SparkSQL一些操作
查看>>
神奇的html5+css3
查看>>
ArcGIS AddIN异常之:object reference not set to an instance of an object
查看>>
Codeforces 776E The Holmes Children
查看>>
idea开发环境中maven控制台乱码解决
查看>>
git重写历史记录
查看>>
拷贝 vs 赋值
查看>>