Binary vs non-binary
quinn at riyal.ugcs.caltech.edu
Sun Nov 28 03:22:57 CET 1999
On Sat, 27 Nov 1999 10:35:04 +0100, Gerrit Holl <gerrit.holl at pobox.com> wrote:
>can someone explain me the difference in opening a file in binary mode
>and opening it not in binary mode? And when is some file defined to be
>binary, when it contains an ASCII 0? If so, I understand you need to say
>it's binary in C, but if Python doesn't store it strings with an ASCII 0
>on the end, why is there any difference?
This is an artifact of C having grown up on unix, where lines end with \n,
and then being used on other systems, where lines end with \r\n (also, many
network protocols use \r\n for some reason). Anyway, if you open a file
in C on, say, DOS, there is going to be an inconsistency because C wants
to think of lines being terminated by single characters. So stdio does some
behind the scenes munging to turn all \r\ns into \ns and vice-versa. But if
you did that on binary data, you'd corrupt it because \n is just another bit
pattern there. So you need to know whether you're opening a binary file or
not. And since python is written in C..
Basically: under unix and other systems that use \n, you don't have to worry
about binary mode---setting it does nothing. But if you want your programs to
be portable to DOS (and probably MacOS too, which uses \r), you should use
binary mode when opening binaries.
It has nothing to do with ASCII 0 (NUL), although it's a good thing to
notice: many C programs choke on input with NULs in it.
More information about the Python-list