我想逐个字节读取一个文件,并检查每个字节的最后一位是否设置:
#!/usr/bin/python
def main():
fh = open('/tmp/test.txt', 'rb')
try:
byte = fh.read(1)
while byte != "":
if (int(byte,16) & 0x01) is 0x01:
print 1
else:
print 0
byte = fh.read(1)
finally:
fh.close
fh.close()
if __name__ == "__main__":
main()
我得到的错误是:
Traceback (most recent call last):
File "./mini_01.py", line 21, in
main()
File "./mini_01.py", line 10, in main
if (int(byte,16) & 0x01) is 0x01:
ValueError: invalid literal for int() with base 16: '\xaf'
任何一个想法?我没有使用struct和binascii模块成功.提前Thx!