Printing unix Line endings from Windows.

Discussion in 'Python' started by Ant, Dec 4, 2006.

  1. Ant

    Ant Guest

    Hi all,

    I've got a problem here which has me stumped. I've got a python script
    which does some text processing on some files and writes it back out to
    the same file using the fileinput module with inplace set to True.

    The script needs to run from Windows, but the files need to be written
    with Unix line endings.

    Is there any way of doing this without having to post-process the file
    in binary mode (a-la the crlf.py script)

    Cheers,
    Ant, Dec 4, 2006
    #1
    1. Advertising

  2. Ant

    Larry Bates Guest

    Ant wrote:
    > Hi all,
    >
    > I've got a problem here which has me stumped. I've got a python script
    > which does some text processing on some files and writes it back out to
    > the same file using the fileinput module with inplace set to True.
    >
    > The script needs to run from Windows, but the files need to be written
    > with Unix line endings.
    >
    > Is there any way of doing this without having to post-process the file
    > in binary mode (a-la the crlf.py script)
    >
    > Cheers,
    >

    You can write to a new file and create your own line endings.
    When done, delete the original file and rename the output file.

    -Larry
    Larry Bates, Dec 4, 2006
    #2
    1. Advertising

  3. Ant

    Ant Guest

    Larry Bates wrote:

    > Ant wrote:

    ....
    > > Is there any way of doing this without having to post-process the file
    > > in binary mode (a-la the crlf.py script)

    ....
    > You can write to a new file and create your own line endings.
    > When done, delete the original file and rename the output file.


    How can I create my own line endings? I've tried setting os.linesep =
    "\n", (and to \x0a). I've tried things like:

    print "xxx yyy \n",
    print "xxx uuu \x0a",
    filehandle.write("xxx \n")
    filehandle.write("xxx \x0a")

    and all of these give me a nice windows-style crlf!

    Surely there must be a way to do this ...
    Ant, Dec 5, 2006
    #3
  4. Ant wrote:

    > How can I create my own line endings? I've tried setting os.linesep =
    > "\n", (and to \x0a). I've tried things like:
    >
    > print "xxx yyy \n",
    > print "xxx uuu \x0a",
    > filehandle.write("xxx \n")
    > filehandle.write("xxx \x0a")
    >
    > and all of these give me a nice windows-style crlf!
    >
    > Surely there must be a way to do this ...


    endline normalization is done by the file object, on the way out. to
    switch this off, open the output file in binary mode ("wb").

    </F>
    Fredrik Lundh, Dec 5, 2006
    #4
  5. Ant

    John Machin Guest

    Ant wrote:
    > Larry Bates wrote:
    >
    > > Ant wrote:

    > ...
    > > > Is there any way of doing this without having to post-process the file
    > > > in binary mode (a-la the crlf.py script)

    > ...
    > > You can write to a new file and create your own line endings.
    > > When done, delete the original file and rename the output file.

    >
    > How can I create my own line endings? I've tried setting os.linesep =
    > "\n", (and to \x0a). I've tried things like:
    >
    > print "xxx yyy \n",
    > print "xxx uuu \x0a",
    > filehandle.write("xxx \n")
    > filehandle.write("xxx \x0a")
    >
    > and all of these give me a nice windows-style crlf!
    >
    > Surely there must be a way to do this ...


    and there is: open your output file in binary mode; then it won't
    convert every \n to \r\n.

    writing:
    | >>> f = open('unixlf.txt', 'wb')
    | >>> f.write('foo\n')
    | >>> f.write('bar\n')
    | >>> f.close()

    checking:
    | >>> f = open('unixlf.txt', 'rb')
    | >>> x = f.read()
    | >>> x
    | 'foo\nbar\n'
    | >>> len(x)
    | 8

    BTW:
    | >>> '\n' is '\x0a'
    | True

    HTH,
    John
    John Machin, Dec 5, 2006
    #5
  6. Ant

    Ant Guest

    John Machin wrote:

    > Ant wrote:

    ....
    > > filehandle.write("xxx \n")
    > > filehandle.write("xxx \x0a")
    > >
    > > and all of these give me a nice windows-style crlf!
    > >
    > > Surely there must be a way to do this ...

    >
    > and there is: open your output file in binary mode; then it won't
    > convert every \n to \r\n.

    ....
    > | >>> '\n' is '\x0a'
    > | True


    (and F wrote something very similar.)

    Cheers guys. Shame that fileinput doesn't take an argument to specify
    the write mode when 'inplace' is True, because it is otherwise makes
    editing multiple files in place very simple.

    Is it worth me submitting a patch to fileinput which can take an
    optional write mode parameter?

    Cheers,
    Ant, Dec 5, 2006
    #6
  7. Ant wrote:

    > Is it worth me submitting a patch to fileinput which can take an
    > optional write mode parameter?


    absolutely.

    </F>
    Fredrik Lundh, Dec 5, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    4
    Views:
    560
    Fuzzyman
    Feb 7, 2006
  2. Fuzzyman

    Detecting line endings

    Fuzzyman, Feb 6, 2006, in forum: Python
    Replies:
    18
    Views:
    584
    Fuzzyman
    Feb 8, 2006
  3. Replies:
    5
    Views:
    391
    Marc 'BlackJack' Rintsch
    May 3, 2007
  4. Steven D'Aprano
    Replies:
    1
    Views:
    343
    Patrick Maupin
    Apr 11, 2010
  5. Brian & Colleen

    Active State Perl with Unix-line endings -- Help

    Brian & Colleen, Oct 16, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    152
    Brian & Colleen
    Oct 16, 2004
Loading...

Share This Page