[Pythonmac-SIG] Filetypes.

Chris Barker chrishbarker@home.net
Thu, 07 Jun 2001 11:27:41 -0700


Russell E Owen wrote:

> So apparently your python and gnuplot have different ideas about line
> ending. If possible, I'd rebuild whichever package is in the less
> desirable mode (or is easier to rebuild) so they both match, as it'd
> save a lot of headaches to have consistency.

That'd be great, but re-building GNUPLOT may not be so easy. IT would be
worth checking out he GNUPLOT lists and see what people are doing on
OS-X, someone may have done this.
 
> But it's certainly trivial to write files using any line ending you
> like. Just write out the data using write (or print with a final
> comma, to suppress the automatic line ending) and include "\n" or
> "\r" as desired. I think you may also need to open the file in binary
> mode (e.g. outfile = open(outfilename, "rb")), as otherwise you may
> get automatic line ending translation.

Yes, you certainly do need to open it as a binary file. Text files do
get line endings translated automatically. AS it happens, on writing,
this means that "\n"s are converted, so if you write a file with no
"\n"s (only "\r"s), then it should work fine, but it's really a good
habit to open files as binary if you don't want linefeed translation
(this has bitten me a few times)


> > Are there any special flags for open()?

well, no, but wouldn't that be nice! There was ssome discussion on this
list and Python-dev about making Python text file reading transparent to
what type of line endings the file has. It was decided that it would be
a nice idea, but ot get performance, you would need to write some very
low level code, so who knows if anyone will get around to it. I amy
write a PEP about it if I get the chance.

In the meantime, if you don't care about performance, I have written a
pure Python module that allows you to do just what you want, open a text
file for writing, and specify the type of line endings you want. It will
also read Mac, DOS, or UNIX style text files for you transparently. The
resulting file acts just like a regular file, so you can pass it to
third party modules, etc.

Let me know if you want it, I'll send you a copy.

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker@home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------