How to print to stdout in binary

Adam DePrince deprince at sprynet.com
Sat Jul 8 13:04:00 EDT 2000


pehr anderson wrote:
> 
> What platform are you developing on?
> under linux I did the following in a file "t.py":

<snip here to save bandwidth ... 

>
> >
> > Thanks
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.


Python's "open" is a wrapper around the C-lib's "fopen" function.  C
wants new lines to be represented as a "\n" (ASCII dec 10)  Most Unixes
and other non-MS operating systems conform to C's nomenclature.

Other operating systems, noteablely Microsoft's stuff, use a two byte
sequence "\r\n" or #13;#10; (or is ot #10;#13;, I forget which order) to
indicate a new line.  It is a _good thing_ for automatic translation to
occur for text files; it is a bad thing for this to happen for binary
files (i.e. the binary format author may not include the notion of
lines, or appreciate their newlines being munged)

Also, some OS's like VMS actually have a special flag in the file's
directory entry indicating if it is a "binary" or "text" file.  

C, in an effort to support all of these "flavors" permits the selection
between text and binary.  The results of this depend on the operating
system, but in general are:


Linux, BSD, all other Unix deriviatives:
The "b" flag has no effect.  

MSWindows:
The "b" flag indicate that automatic translation from window's to C's 
newline convention should _not_ take place.

VMS: 
Set the file's binary flag accordingly.  Does anybody know if VMS does
Windows style translation too?

Or, to quote from Linux's man page (sec 2, open)


       The mode string can also include the letter  ``b''  either
       as  a last character or as a character between the charac<AD>
       ters in any of the two-character strings described  above.
       This  is  strictly for compatibility with ANSI C3.159-1989
       (``ANSI C'') and has no effect; the ``b''  is  ignored  on
       all  POSIX  conforming  systems,  including Linux.  (Other
       systems may treat text files and binary files differently,
       and adding the ``b'' may be a good idea if you do I/O to a
       binary file and expect that your program may be ported  to
       non-Unix environments.)



-- 
Adam DePrince - firstname at lastname dot net, all in lowercase



More information about the Python-list mailing list