非文本文件 读取

# 在操作非文本文件时,必须明确指定模式为字节模式
# b 用来指定为字节模式
#
# 注意
# 1.b 必须与 r\w 连用   rb(readBytes)\wb(writeBytes)
# 2.当模式为字节模式时 不能指定encoding参数!

# 默认情况下是读写文本模式 也就是t模式 同样需要与r\w连用
#  rt(readText)\wt(readText)
# t模式下  python解释器会自动进行编码和解码而b模式不会

# with open("xxx.png",mode="rb") as f:
#     f.read(1024)

# with open(r"D:\sh_fullstack_s6\day8\代码\test.txt",mode="rb") as f:
#     print(f.read(4).decode("utf-8")) # 当模式为字节模式时 单位为字节

# 循环读取全部内容
with open("xxx.png",mode="rb") as f:
    while True:
        data = f.read(1024)
        if not data: # 如果data为空则意味着文件读完了
            break
        print(data)

猜你喜欢

转载自www.cnblogs.com/Hale-wang/p/10354239.html