Python之文件处理
文件的基础操作
应该写文件的绝对路径;如果文件在代码所在的同级目录下,可以直接写文件名
关于文件编码:用什么编码处理,就必须用什么编码打开,否则可能乱码
# 创建一个文件test.txt ,文件内容如:
11111111
22222222
33333333
- open() 函数:打开文件,向操作系统申请系统调用,占用文件句柄,返回值为一个文件对象
可指定读取文件时的权限和编码格式
file = open('test.txt','r',encoding='utf8')
print(file)
# <_io.TextIOWrapper name='test.txt' mode='r' encoding='utf-8'>
- close() 函数:关闭打开的文件,清除占用的文件句柄
file = open('test.txt','r',encoding='utf8')
file.close()
3. with func() as VAR:
打开文件后无需显式调用close()函数来关闭占用句柄
with open('a.txt','w',encoding='gbk') as file:
file.write('警察\n')
with open('a.txt','r',encoding='gbk') as file:
data = file.readlines()
print(data)
# 创建a.txt文件
# ['警察\n']
文件的读操作
- read() 函数:读取文件内容,且该函数返回值为读取到的文件内容
读取完毕后,光标移动到文件末尾
文件读取完毕后应调用 close() 函数来关闭读取的文件,清除占用的文件句柄
file = open('test.txt','r',encoding='utf8')
data = file.read()
print(data)
file.close()
# 11111111
# 22222222
# 33333333
- readable() 函数:判断文件是否可读,函数的返回值为True或False
file = open('test.txt','r',encoding='utf-8')
data1 = file.readable()
print(data1)
# True
- readline() 函数:逐一扫描并读取文件的一行
读取一行默认换行,可增加 ” end=” ” 参数,可去掉换行
file = open('test.txt','r',encoding='utf-8')
print('第一行:',file.readline())
print('第二行:',file.readline(),end='')
print('第三行:',file.readline(),end='')
print('第四行:',file.readline(),end='')
# 第一行: 11111111
#
# 第二行: 22222222
# 第三行: 33333333第四行:
- readlines() 函数:
file = open('test.txt','r',encoding='utf-8')
print(file.readlines())
# ['11111111\n', '22222222\n', '33333333\n']
文件的写操作
如果文件存在,则清空文件,准备写入内容
如果文件不存在,则创建一个空文件,准备写入内容
file = open('test.txt','w',encoding='utf8')
print(file)
print(file.readable())
# <_io.TextIOWrapper name='test.txt' mode='w' encoding='utf8'>
# False
- write() 函数:向文件中写入内容
仅能向文件中写入 str 类型一种数据
注意要加换行符 ” \n ” 才能换行
file = open('test.txt','w',encoding='utf8')
file.write('11111')
file.write('\n22222\n')
file.close()
file = open('test.txt','r',encoding='utf8')
print(file.read())
# 自动创建test.txt 文件
# 11111
# 22222
- writable() 函数:测试文件是否可写
file = open('test.txt','w',encoding='utf8')
print(file.writable())
# True
- writelines() 函数:
如果文件存在,会将原有的文件内容覆盖
file = open('test.txt','w',encoding='utf8')
file.writelines(['33333\n','44444'])
file.close()
file = open('test.txt','r',encoding='utf8')
print(file.read())
# 覆盖test.txt 原有内容
# 33333
# 44444
文件的追加操作
如果文件存在,则向文件追加内容
如果文件不存在,则创建一个空文件,向空文件内追加内容
file = open('test1.txt','a',encoding='utf-8')
file.write('11111\n')
file.write('11111\n')
file.close()
file = open('test1.txt','r',encoding='utf-8')
print(file.read())
# 创建test1.txt文件
# 11111
# 11111
文件的修改操作
文件的修改操作本质是:先读取文件(如果文件存在)内容到内存中,在内存中修改后,覆盖原有文件,最终结果看起来像是直接对文件子进行了修改。
with open('a.txt','r',encoding='gbk') as file:
data = file.readlines()
print(data)
with open('a.txt','w',encoding='gbk') as file:
file.writelines([data[0],'抓小偷'])
# a.txt文件内容:
# 警察抓小偷
文件的 b (binary:二进制)模式
二进制模式无需指定字符串编码格式,否则报错
我们看到的字符串与磁盘上存储的二进制之间必须通过编码格式来“编码”或“解码”转换
- 将字符串装换成二进制有两种方法,此处更推荐使用encode() 函数
(1). bytes() 函数
格式:bytes('str',encoding='FORMAT')
用法如:
a = bytes('abc警察',encoding='utf-8')
print(type(a),'\n',a)
# <class 'bytes'>
# b'abc\xe8\xad\xa6\xe5\xaf\x9f'
(2). encode() 函数
格式:'str'.encode('FORMAT')
用法如:
b = 'abc小偷'.encode('utf-8')
print(type(b),'\n',b)
# <class 'bytes'>
# b'abc\xe5\xb0\x8f\xe5\x81\xb7'
- 文件的 rb 模式
二进制模式无需指定字符串编码格式,否则报错
注:windows系统中的换行符为 “\r\n”
with open('test.txt','rb') as f:
data = f.read()
print(data)
# b'11111\r\n22222\r\n33333\r\n44444\r\n\xe8\xad\xa6\xe5\xaf\x9f'
decode() 函数:python内置函数
# test.txt 文件的编码格式为 utf-8 ,解码时必须也使用 utf-8
with open('test.txt','rb') as f:
data = f.read()
print(data.decode('utf-8'))
# 11111
# 22222
# 33333
# 44444
# 警察
- 文件的 wb 模式
二进制模式无需指定字符串编码格式,否则报错
with open('aaa.txt','wb') as f:
data = f.write('警察抓小偷'.encode('utf-8'))
# 测试写入的结果
with open('aaa.txt','r',encoding='utf-8') as f1:
print(f1.read())
# 警察抓小偷
- 文件的 ab 模式
二进制模式无需指定字符串编码格式,否则报错
with open('aaa.txt','ab') as f:
data = f.write('警察抓小偷'.encode('utf-8'))
# 测试写入的结果
with open('aaa.txt','r',encoding='utf-8') as f1:
print(f1.read())
# 警察抓小偷警察抓小偷
文件的其他模式
file.name() 文件名
file.closed() 判断是否已关闭
file.flush() 将内存中的内容刷到磁盘
file.tell() 显示当前光标位置
file.seek() 移动光标,跳过多少字节(不是字符)
正向,不记忆光标位置:file.seek(int,0)
正向,记忆光标位置 :file.seek(int,1)
逆序,file.seek(int,2)file.read() 会使光标移动至文件末尾,故不可混用。
-
- file.seek(int,0) 函数:正向,不记忆光标位置
with open('aaa.txt','w',encoding='utf-8') as f:
f.write('11111\n22222\n')
with open('aaa.txt','r',encoding='utf-8') as f:
print('当前光标位置:',f.tell())
print(f.read())
f.seek(3) # 等同于 f.seek(3,0)
print('当前光标位置:',f.tell())
print(f.read())
f.seek(10) # 等同于 f.seek(10,0)
print('当前光标位置:',f.tell())
print(f.read())
# 当前光标位置: 0
# 11111
# 22222
# 当前光标位置: 3
# 11
# 22222
# 当前光标位置: 10
# 22
-
- file.seek(int,1) 函数:正向,记忆光标位置
注意:必须在 ” rb ” 模式下
with open('aaa.txt','w',encoding='utf-8') as f:
f.write('11111\n22222\n')
with open('aaa.txt','rb') as f:
print('当前光标位置:',f.tell())
f.seek(3,1)
print('当前光标位置:',f.tell())
f.seek(10,1)
print('当前光标位置:',f.tell())
# 当前光标位置: 0
# 当前光标位置: 3
# 当前光标位置: 13
-
- file.seek(int,2) 函数:逆序移动光标
注意:必须在 ” rb ” 模式下
注意:逆序移动光标,移动的字节数必须是 负数 !
with open('aaa.txt','w',encoding='utf-8') as f:
f.write('11111\n22222\n')
with open('aaa.txt','rb') as f:
print('当前光标位置:',f.tell())
f.seek(-6,2)
print('当前光标位置:',f.tell())
print(f.read())
# 当前光标位置: 0
# 当前光标位置: 8
# b'2222\r\n'
file.truncate() 保留截断的字节(不是字符)
必须以写模式打开,但是不能使用w和w+模式,可以使用wb,r+等其他模式
with open('aaa.txt','w',encoding='utf-8') as f:
f.write('11111\n22222\n')
with open('aaa.txt','wb') as f: #也可以使用 r+ 模式
print('当前光标位置:',f.tell())
print(f.truncate(10))
# 10