文章目录
文件操作
f = open("文件名",mode="采用的模式",encodng="使用什么编码集")
f 这个变量会接受到 open的返回值 是一个文件io对象 (又称文件句柄)
i => input 输入
o => output 输出
有了对象之后,就可以使用对象.属性 或者 对象.方法进行操作.
文件的写入和读取
# 写入文件
# 打开文件
fp = open("0506.txt",mode="w",encoding="utf-8")# 把冰箱门打开
# 写入文件
fp.write("把大象放到冰箱里面")# 把大象塞进去
# 关闭文件
fp.close()# 把冰箱门关上
# 读取文件
# 打开文件
fp = open("0506.txt",mode="r",encoding="utf-8")# 把冰箱门打开
# 读取文件
res = fp.read()# 把大象从冰箱里面拿出来
# 关闭文件
fp.close()# 把冰箱门关上
print(res)
将字符串和字节流(Bytes流)类型进行转换 (参数写成转化的字符编码格式)
- encode() 编码 将字符串转化为字节流(Bytes流)
- decode() 解码 将Bytes流转化为字符串
strvar = '守望'
byt = strvar.encode('utf-8')
print(byt)
res_2 = byt.decode('utf-8')
print(res_2)
wb 和 rb 模式
(注意点:使用字节流模式的时候,不要指定encoding)
# 文件的写入
f = open('234.txt', mode='wb')
strvar = '多想再见你'
res = strvar.encode('utf-8')
f.write(res)
f.close()
# 文件的读取
f = open('234.txt', mode='rb')
res = f.read()
f.close()
print(res)
res_2 = res.decode('utf-8')
print(res_2)
复制图片
(图片或者视频之类的二进制字节流的内容 , 都可以使用b模式 比如wb , rb …)
# 打开文件
fp = open("ceshi.png",mode="rb")
# 读取文件
res = fp.read()
# 关闭文件
fp.close()
# 打开文件
fp = open("ceshi2.png",mode="wb")
# 写入文件
fp.write(res)
# 关闭文件
fp.close()
扩展模式 +
w+ r+ a+
(utf-8编码格式下 默认一个中文三个字节 一个英文或符号 占用一个字节)
- read() 功能: 读取字符的个数(里面的参数代表字符个数)
- seek() 功能: 调整指针的位置(里面的参数代表字节个数)
- 把光标移动到文件行首f.seek(0)
- 把光标移动到文件末尾f.seek(0,2)
- tell() 功能: 当前光标左侧所有的字节数(返回字节数)
r+ 可读可写 (先读后写)
f = open('234.txt', mode='r+', encoding='utf-8')
res = f.read()
print(res)
f.write('哪怕匆匆一眼就别离')
# 把光标移动到第0个字节上,那就是文件开头
f.seek(0)
res = f.read()
print(res)
f.close()
r+ 可读可写 (先写后读)
f = open('234.txt', mode='r+', encoding='utf-8')
# 把光标移动到文件末尾
f.seek(0, 2)
f.write('123')
f.seek(0)
res = f.read()
print(res)
f.close()
w+ 可读可写
f = open('test_1.txt', mode='w+', encoding='utf-8')
f.write('abcdef')
f.seek(0)
res = f.read()
print(res)
f.close()
a+ 可读可写
append (写入的时候,强制把光标移动到最后)
# 有中文的情况,如果移动错误可能读取不出来
# 一个中文三个字节
f = open('test_1.txt', mode='a+', encoding='utf-8')
f.seek(0)
f.write(",平安京hijk")
f.seek(0)
res = f.read()
print(res)
f.close()
tell read seek
注意 seek括号里的是字节数 read括号里的是字符数
f = open('test_1.txt', mode='a+', encoding='utf-8')
res = f.tell()
print(res)
f.seek(5)
res = f.tell()
print(res)
res = f.read(2)
res = f.tell()
print(res)
f.close()
f = open('test_1.txt', mode='a+', encoding='utf-8')
res = f.tell()
print(res)
f.seek(2)
res = f.tell()
print(res)
f.read(2)
res = f.tell()
print(res)
f.seek(5)
res = f.tell()
print(res)
res = f.read()
f.close()
with 操作
with 语法 自动关闭文件 相当于帮你执行了fp.close()
with open('test_2.txt', mode='w', encoding='utf-8') as f:
f.write('七月七日长生殿\n')
文件相关操作函数
flush
四种情况下刷新缓冲区:
- 当文件关闭的时候自动刷新缓冲区
- 当整个程序运行结束的时候自动刷新缓冲区
- 当缓冲区写满了 会自动刷新缓冲区
- 手动刷新缓冲区(flush)
f = open('test_7.txt',mode='w',encoding='utf-8')
f.write('让我们\n\t红尘作 伴 \n\t活得潇潇洒洒\n\t\t 策马奔腾\n')
# 手动刷新缓冲区
fp.flush()
# 死循环 导致不能正常关闭 或者 程序结束 只能手动刷新
while True:
pass
f.close()
文件对象具有可迭代性
- readable() 功能: 判断文件对象是否可读
- writable() 功能: 判断文件对象是否可写
f = open('test_7.txt',mode='r',encoding='utf-8')
res_1 = f.readable()
res_2 = f.writable()
print(res_1)
print(res_2)
for i in f:
print(i)
f.close()
readline()
功能: 读取一行文件内容
readline(字符数)
- 如果给的参数大于当前行字符数,只获取当前行所有内容
- 如果给的参数小于当前行字符数,按照实际给的参数进行字符的获取
with open('test_7.txt',mode='r+',encoding='utf-8') as f:
res = f.readline()
while res:
print(res)
res = f.readline()
f.seek(0)
res_2 = f.readline(2)
print(res_2)
readlines()
功能:将文件中的内容按照换行读取到列表当中
lst = []
with open('test_3.txt',mode='r+',encoding='utf-8') as f:
res = f.readlines()
for i in res:
res_2 = i.strip()
lst.append(res_2)
print(lst)
writelines()
功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据
with open('test_3.txt',mode='r+',encoding='utf-8') as f:
strvar = '忽然下起了大雪'
lst = ['若不是\n','你忽然\n','闯进\n','我生活\n']
f.writelines(lst)
truncate()
功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)
with open('test_3.txt',mode='r+',encoding='utf-8') as f:
f.truncate(26)
注意:
read(字符)
readline(字符)
seek(字节)
truncate(字节)
tell返回的是字节