文件的操作是必备技能啊。
1 文件内容基本操作
# 打开文件
file_object = open('路径',mode = '模式',encoding = '编码')
file_object = open('log.txt',mode = 'r',encoding = 'utf-8')
# 操作
content = file_object.read()
print(content)
# 关闭文件
file_object.close()
2 打开模式
■ r / w / a 【只读只写字符串】 常用
注意:
r resd | 只读 | 若无文件,不能新建,会报错 |
---|---|---|
w write | 只写 | 写之前会将所有内容清空,若无文件,能新建 |
a append | 追加 | 在文件的尾部进行追加,若无文件,能新建 |
■ r+ / w+ / a+ 【可读可写字符串】
注意:
r+ | 可读可写 | 注意光标的位置 |
---|---|---|
w+ | 可读可写 | 写入时会将文件的内容清空,读取时需调整光标的位置 |
a+ | 可读可写 | 一打开文件,光标就移动到最后,若要读取内容,需调整光标的位置,但只要写,光标就会自动跳到最后 |
■ rb / wb / ab 【只读只写二进制】 常用
1.字符串转换二进制
# 字符串转换二进制
v = '你好'
data = v.encode('utf-8')
print(data) # b'\xe4\xbd\xa0\xe5\xa5\xbd'
2.二进制转换字符串
# 二进制转换字符串
v = b'\xe6\xb1\x89\xe5\xad\x97'
data = v.decode('utf-8')
print(data) # 汉字
■ r+b / w+b / a+b 【可读可写二进制】
3 操作
3.1.读操作
file_object = open('log.txt',mode = 'r',encoding = 'utf-8')
# content = file_object.read() #读取文件的所有内容到内存
# content = file_object.read(2) #从当前光标所在的位置向后读取文件两个字符
content_list = file_object.readlines() #读取文件的所有内容到内存,并按照每一行进行分割到列表中。得到的是列表
print(content)
file_object.close()
3.2.写操作
#写操作
file_object = open('log.txt',mode = 'w',encoding = 'utf-8')
# file_object.write('你好\n') #可以换行
file_object.write('小黑')
file_object.write('888')
file_object.close() # 写完以后,一定要关闭文件
# 写操作的两种方法:
# 方法一:
f = open('hello.txt',mode = 'w',encoding = 'utf-8')
f.write('hello')
f.close()
# 方法二:
f = open('hello.txt',mode = 'wb')
data = '你好'
content = data.encode('utf-8')
print(content)
f.write(content)
f.close()
3.3.可读可写操作
# 可读可写1
# 打开文件
file_object = open('log.txt',mode = 'r+',encoding = 'utf-8')
file_object.seek(9) #光标移动到第3个字节后面,也就是一个汉字。.seek() 是调整光标的位置
file_object.write('666') #根据光标的位置,从当前光标位置开始进行写入操作(可能会将其他的文字覆盖)
# 读取内容
file_object.seek(0)
content = file_object.read()
print(content)
file_object.write('可读可写')
# 关闭文件
file_object.close()
# 可读可写2
# 打开文件
file_object = open('log.txt',mode = 'w+',encoding = 'utf-8') #打开文件会清空,因此读出来是空
file_object.write('666') # 写完后光标在最后,若要读文件,也是空,因此,需要调整光标的位置
file_object.seek(0)
content = file_object.read()
print(content)
file_object.close()
# 可读可写3
# 打开文件
file_object = open('log.txt',mode = 'a+',encoding = 'utf-8') #打开文件时光标会移动到最后,若要读文件,需要调整光标位置
file_object.write('888') # 不管光标位置此时在哪里,只要开始写,光标自动跳到末尾
file_object.seek(0)
content = file_object.read()
print(content)
file_object.close()
■ seek(光标字节位置),无论模式是否带b,都是按照字节进行处理。
■ tell(),获取光标当前所在的字节位置
# # tell(),获取光标当前所在的字节位置
obj = open('log.txt',mode = 'rb') #666888
# obj.seek(3) #跳转到指定字节位置
obj.read() # 全部读取
data = obj.tell()
print(data)
obj.close()
■ flush(),强制将内存中的数据写入到硬盘
# flush(),强制将内存中的数据写入到硬盘
v = open('log.txt',mode = 'a',encoding = 'utf-8')
while True:
val = input('请输入内容:')
v.write(val)
v.flush()
if val == 'n':
break
v.close()
4 关闭文件
file_object = open('log.txt',mode = 'r',encoding = 'utf-8')
file_object.close()
5 文件内容的修改
# 文件内容修改,目前只支持以下操作:先全部读取,修改后,再将修改后的内容重新写入
v = open('log.txt',mode = 'r',encoding = 'utf-8')
data = v.read()
new_data = data.replace('666','你好')
v.close()
v = open('log.txt',mode = 'w',encoding = 'utf-8')
data = v.write(new_data)
v.close()
对大文件进行修改
# 如果以后读取一个特别大的文件
file_object = open('log.txt',mode = 'r',encoding = 'utf-8')
for line in file_object:
line = line.strip() #.strip()不仅能清除两边的空白,还可以清除换行字符
print(line)
file_object.close()