flush刷新缓冲区的问题
"""
fp = open("文件名",mode="模式",encoding="utf-8")
fp -> 文件的io对象 (文件句柄)
i => input 输入
o => output 输出
"""
fp = open("ceshi1.txt",mode="w",encoding="utf-8")
fp.write("把大象塞进去")
fp.close()
fp = open("ceshi1.txt",mode="r",encoding="utf-8")
res = fp.read()
print(res)
fp.close()
"""
bytes : 是用来传输或者存储的数据格式
b'1234' b"abcd" b"我爱你" -> b开头的字节流,范围只能是ascii编码
如果是中文使用encode 和 decode 来进行转换;
# 将字符串和字节流(Bytes流)类型进行转换 (参数写成转化的字符编码格式)
#encode() 编码 将字符串转化为字节流(Bytes流)
#decode() 解码 将Bytes流转化为字符串
"""
a = b'1234'
print(a , type(a))
strvar = "我爱你"
res = strvar.encode("utf-8")
print(res , type(res))
res2 = res.decode("utf-8")
print(res2 , type(res2))
num = len(res)
print(num)
res3 = b"\xe7\x88\xb1".decode("utf-8")
print(res3)
strvar = "我!是你一辈子也得不到的男人"
strvar2 = strvar.encode()
print(strvar2)
"""不需要指定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)
strvar = res.decode()
print(strvar)
"""图片,音频,视频"""
fp = open("集合.png",mode="rb")
res = fp.read()
fp.close()
fp = open(r"E:\python31\day8\集合3.png",mode="wb")
fp.write(res)
fp.close()
"""
seek(0) 直接把光标移动到文件开头
seek(0,2) 直接把光标移动到文件末尾
"""
"""
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()
"""
"""
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()
"""
"""
fp = open("ceshi4.txt",mode="w+",encoding="utf-8")
fp.write("abc")
fp.seek(0)
res = fp.read()
print(res)
fp.close()
"""
"""文件不存在时,默认创建新的文件"""
"""
fp = open("ceshi5.txt",mode="a+",encoding="utf-8")
fp.write("123")
fp.seek(0)
res = fp.read()
print(res)
# a模式在写入内容时,会强制把光标移动到最后
fp.seek(1)
fp.write("abc")
fp.close()
# 如果在r模式内,区别a模式
fp = open("ceshi5.txt",mode="r+",encoding="utf-8")
fp.seek(1)
fp.write("abc")
fp.close()
"""
"""
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
res = fp.read(3)
print(res)
fp.seek(6)
print(fp.read(1))
# 计算文件指针左侧所有的字节数
res = fp.tell()
print(res)
fp.close()
"""
"""
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.seek(2)
res = fp.read()
print(res)
fp.close()
# print("我".encode())
# b'\xe6\x88\x91'
"""
with open("集合.png",mode="rb") as fp:
res = fp.read()
with open(r"E:\python31\day8\集合4.png",mode="wb") as fp:
fp.write(res)
with open("集合.png",mode="rb") as fp1 , open(r"E:\python31\day8\集合5.png",mode="wb") as fp2:
res = fp1.read()
fp2.write(res)
"""
# 刷新缓冲区 flush
# 当文件关闭的时候自动刷新缓冲区
# 当整个程序运行结束的时候自动刷新缓冲区
# 当缓冲区写满了 会自动刷新缓冲区
# 手动刷新缓冲区
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.write("zzz")
# 手动把缓冲区里面的内容写入文件当中
fp.flush()
while True:
pass
fp.close()
"""
fp = open("ceshi6.txt",mode="a+",encoding="utf-8")
res = fp.readable()
print(res)
res = fp.writable()
print(res)
"""
参数 > 当前行字符总个数 => 以当前行读取
参数 < 当前行字符总个数 => 以参数的大小来读取字符的个数
默认readline 读取一行
"""
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:
'''在遍历文件对象的时候,默认一次拿一行'''
for i in fp:
print(i)
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:
res = fp.readline()
while res:
print(res)
res = fp.readline()
lst_new = []
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:
lst = fp.readlines()
for i in lst:
lst_new.append(i.strip())
print(lst_new)
"""可迭代型数据(容器类型数据,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)
"""
with open("ceshi8.txt",mode="r+",encoding="utf-8") as fp:
fp.truncate(3)