Add 'b'
to the file mode. 'b'
is a mode that means binary mode.
The relevant modes are available in open.
with open("myfile", "rb") as f:
byte = f.read(1)
while byte != "":
# Byte Processing
byte = f.read(1)
The with
statement does not support Python version 2.5 or lower, so you may need to add from__future_import with_statement
You can't read raw characters in Python 3, so you need to change the conditional statements("b""
in "
with open("myfile", "rb") as f:
byte = f.read(1)
while byte != b"":
# # Do stuff with byte.
byte = f.read(1)
### or
with open("myfile", "rb") as f:
byte = f.read(1)
while byte:
# # Do stuff with byte.
byte = f.read(1)
© 2024 OneMinuteCode. All rights reserved.