读取二进制文件并遍历每个字节在Python中,如何读取二进制文件并遍历该文件的每个字节?
3 回答

阿晨1998
TA贡献2037条经验 获得超6个赞
Python 2.4及更高版本
f = open("myfile", "rb")try: byte = f.read(1) while byte != "": # Do stuff with byte. byte = f.read(1)finally: f.close()
Python 2.5-2.7
with open("myfile", "rb") as f: byte = f.read(1) while byte != "": # Do stuff with byte. byte = f.read(1)
from __future__ import with_statement
Python 3
with open("myfile", "rb") as f: byte = f.read(1) while byte != b"": # Do stuff with byte. byte = f.read(1)
b""
with open("myfile", "rb") as f: byte = f.read(1) while byte: # Do stuff with byte. byte = f.read(1)
添加回答
举报
0/150
提交
取消