how exactly do binary files work in python?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Mar 13 05:06:09 CET 2006


On Sun, 12 Mar 2006 22:01:46 -0500, John Salerno wrote:

> Erik Max Francis wrote:
> 
>> You can use the struct module for converting fundamental types to a 
>> portable string representation for writing to binary files.
> 
> But if it's a string, why not just use a text file? What does a binary 
> file do that a text file doesn't, aside from not converting the end of 
> line characters?

Nothing. It is all bytes under the hood.

People generally consider a file to be "text" if it only includes bytes 32
through 126, plus a few control characters like 9 (tab) and 10 (newline).
Other applications don't care what bytes are included. Python is (mostly)
like that: you can deal with any bytes you collect from any file.

Other than this informal difference between text an binary, the major
difference comes about when you read lines from a text file. Each
operating system has a line separator: Unix/BSD/Linux systems use newline
(char 10), classic Macintosh used to use carriage return (char 12) and
DOS/Windows uses a two-byte carriage return + newline.

When writing lines to a file, Python does not automatically append the
line marker, so you need to do so yourself. But some other languages do --
I believe C++ is one of those languages. So C++ needs to know whether you
are writing in text mode so it can append that end-of-line maker, or
binary mode so it doesn't. Since Python doesn't modify the line you write
to the file, it doesn't care whether you are writing in text or binary
mode, it is all the same.

Operating systems such as Unix and Linux don't distinguish between binary
and text mode, the results are the same. I'm told that Windows does
distinguish between the two, although I couldn't tell you how they
differ.

-- 
Steven.




More information about the Python-list mailing list