python--005--文件操作(b,其他)

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'
hello1
2222
3333
4444
你好啊佩奇
#  ===========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你好啊11
11你好11
#  ===========ab模式 (写到最后一个位置)============
f = open('test33.py','ab')
f.write('11你好11\n'.encode('utf-8'))

output:
test33.py:
11你好啊11
11你好11
11你好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或False
print(f.encoding)    # encoding 表示文件打开的编码
f.close()

output:
False
utf-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\n

output:
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:
0
8    # 注:换行为\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:
0
3
 
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
 3

seek.txt
hello
你好
123
123
f = open('seek.txt','rb')
f.seek(3,1)  #以1的位置开始,即文件相对位置 ,注意:单位是字节,所以必须以rb的方式打开
print(f.tell())
f.seek(10,1)  #
print(f.tell())

output:
3
13
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)

   

猜你喜欢

转载自www.cnblogs.com/jinf/p/10928785.html