How to write dos newline in unix?

Skip Montanaro skip at pobox.com
Wed Jun 9 15:03:38 EDT 2004


    Inyeol> Is there any simple way to dos-style newline (\r\n) in *nix
    Inyeol> environment, not by writing "\r" explicitly? What I'm trying to
    Inyeol> do is;

    Inyeol> ofile = file("foo", "w")
    Inyeol> print "hello, world"

    Inyeol> Then it writes "hello, world\r\n".

You can do it with print:

    ofile = file("foo", "wb")
    print >> ofile, "hello, world\r"

Note the 'b' in the mode.  Not necessary on Unix, but would be on Windows to
get precise control of EOL characters.

I recommend that if you want more precise control over output formatting
that you use the file object's write method instead of the print statement:

    ofile = file("foo", "wb")
    ofile.write("hello, world\r\n")

Skip

    




More information about the Python-list mailing list