Reading binary files

F

Fredrik Lundh

amfr said:
On windows, is there anything special I have to do to read a binary
file correctly?

the documentation has the answer:

http://docs.python.org/lib/built-in-funcs.html#l2h-25

Append 'b' to the mode to open the file in binary mode, on
systems that differentiate between binary and text files (else it
is ignored). /.../

When opening a binary file, you should append 'b' to the
mode value for improved portability. (It's useful even on
systems which don't treat binary and text files differently,
where it serves as documentation.)

in other words, always use

f = open(filename, "rb")

to open binary files for reading, on all platforms (but it only matters
on some platforms). to open for writing, use

f = open(filename, "wb") # create new file

or

f = open(filename, "r+b") # open existing file for read/write

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top