一.文件操作
文件的写入
# 1.打开文件
fp = open("ceshi1.txt",mode="w",encoding="utf-8") # 把冰箱门打开
# 2.写入内容
fp.write("把大象塞进去") # 把大象放进去
# 3.关闭文件
fp.close() # 把冰箱门关上
文件的读取
# 1.打开文件
fp = open("ceshi1.txt",mode="r",encoding="utf-8") #1.打开冰箱门
# 2.读取内容
res = fp.read() # 把大象拿出来
print(res)
# 3.关闭文件
fp.close() # 把冰箱门关上
字节流的转换
bytes : 是用来传输或者存储的数据格式
b’1234’ b"abcd" b"我爱你" -> b开头的字节流,范围只能是ascii编码
如果是中文使用encode 和 decode 来进行转换;
encode() 编码 将字符串转化为字节流(Bytes流)
decode() 解码 将Bytes流转化为字符串
len可以计算字节个数
存储二进制字节流
不需要指定encoding编码集,否则报错
fp = open("ceshi2.txt",mode="wb")
fp.write(strvar2)
fp.close()
读取二进制字节流
fp = open("ceshi2.txt",mode="rb")
res = fp.read()
fp.close()
print(res)
# 通过decode反解出字符串
strvar = res.decode()
print(strvar)
复制图片
“”“图片,音频,视频”""
# 1.读取原图片所有的内容
fp = open("集合.png",mode="rb")
res = fp.read()
fp.close()
# 2.把读取的内容存储到另外一个文件
# fp = open("集合2.png",mode="wb")
# 指定绝对路径(完整路径)
fp = open(r"E:\python31\day8\集合3.png",mode="wb")
fp.write(res)
fp.close()
二.文件的扩展模式
read() 功能: 读取字符的个数(里面的参数代表字符个数)
seek() 功能: 调整指针的位置(里面的参数代表字节个数)
tell() 功能: 当前光标左侧所有的字节数(返回字节数)
seek(0) 直接把光标移动到文件开头
seek(0,2) 直接把光标移动到文件末尾
文件的扩展模式
1.r+ 先读后写
fp = open("ceshi3.txt",mode="r+",encoding="utf-8")
# 先读
res = fp.read()
print(res)
# 后写
fp.write("1234")
# 在读
fp.seek(0) # 调整光标位置在开头
res = fp.read()
print(res)
fp.close()
2.r+ 先写后读
fp = open("ceshi3.txt",mode="r+",encoding="utf-8")
fp.seek(0,2) # 调整光标位置在末尾
fp.write("123")
fp.seek(0)
res = fp.read()
print(res)
fp.close()
3.w+ 可读可写
fp = open("ceshi4.txt",mode="w+",encoding="utf-8")
fp.write("abc")
fp.seek(0)
res = fp.read()
print(res)
fp.close()
4.a+ 可读可写
文件不存在时,默认创建新的文件
a模式在写入内容时,会强制把光标移动到最后
fp = open("ceshi5.txt",mode="a+",encoding="utf-8")
fp.write("123")
fp.seek(0)
res = fp.read()
print(res)
read seek tell 三个函数的使用
注意点,seek移动中文字节的时候,有可能报错
with语法的使用 (close操作with语法可以自动实现)
with open("集合.png",mode="rb") as fp1 , open(r"E:\python31\day8\集合5.png",mode="wb") as fp2:
res = fp1.read()
fp2.write(res)
close 文件关闭的意义
刷新缓冲区 flush
当文件关闭的时候自动刷新缓冲区
当整个程序运行结束的时候自动刷新缓冲区
当缓冲区写满了 会自动刷新缓冲区
手动刷新缓冲区
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.write("zzz")
# 手动把缓冲区里面的内容写入文件当中
fp.flush()
while True:
pass
fp.close()
三.文件的相关函数
1.readable() 功能: 判断文件对象是否可读
2.writable() 功能: 判断文件对象是否可写
3.readline() 功能: 读取一行文件内容
# 读取所有内容
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:
# 先读取一行
res = fp.readline()
# 判断是不是空
while res:
print(res)
res = fp.readline()
4.readlines() 功能:将文件中的内容按照换行读取到列表当中
lst_new = []
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:
lst = fp.readlines()
# print(lst) # ['\t窗前明月光\n', '疑是鞋两双\t\t\n', '\t\t举头王明月\n', '\t低头看裤裆']
for i in lst:
lst_new.append(i.strip())
print(lst_new) # ['窗前明月光', '疑是鞋两双', '举头王明月', '低头看裤裆']
5.writelines() 功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据
可迭代型数据(容器类型数据,range对象,迭代器)
lst = ["春眠不觉晓\n","处处蚊子咬\n","夜来大狗熊\n","一个也跑不了\n"]
*# lst = [1,2,3,4] error*
with open("ceshi8.txt",mode="w",encoding="utf-8") as fp:
fp.writelines(lst)
6.truncate() 功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)
with open("ceshi8.txt",mode="r+",encoding="utf-8") as fp:
fp.truncate(3)