[Pythonmac-SIG] Filetypes.
Russell E Owen
owen@astro.washington.edu
Thu, 7 Jun 2001 10:58:55 -0700
>Hello, all;
>
>I'm running Python-2.1 from the MacOSX terminal app, I just compiled the
>sources from ftp.python.org. Before you yell at me, this is because I want
>to use the ScientificPython package (which requires a C compiler and I'm
>not enough of a mench to learn how to do this...I just want to type 'python
>setup_all.py install' and have it work...).
>
>I've written a bunch of code now and it spits out a Unix two-column ascii
>file. This kind of file, however, is not readable by the Gnuplot
>application for MacOSX, which apparently is expecting Macintosh-type files.
>What's the difference between the two...and how do I write a mac-styled
>file using Python? Are there any special flags for open()?
>
>Thanks...learning slowly.
unix files use \n (newline) as a end of line terminator, but mac
files use \r (return). Mac OS X, being both operating systems, uses
either depending on whether it's a Mac-like application or a
unix-like appliction. (This may be vaguer than reality; I don't run
Mac OS X yet).
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.
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.
If you're not sure which line endings a file has, one easy way to
tell is to open the file in BBEdit. The mode will be listed in one of
the pop-up menus at the top of the window, and will be Mac, unix or
Windows. You can even change it and save the file to convert. Though
if you have a lot of files to convert obviously writing a script or
using a mass conversion file utility makes more sense.
-- Russell