bug? file.write('\n') produces a carriage return and a line feed

Discussion in 'Python' started by jcb@iteris.com, Dec 15, 2005.

  1. Guest

    The following code in Python 2.3
    f = file('t.txt', 'wt')
    f.write('\n')
    f.close()

    writes
    0D0A to the file.

    Is this a bug or expected behavior?
    It sure took me by surprise.

    If it is not a bug, is this behavior documented?
     
    , Dec 15, 2005
    #1
    1. Advertising

  2. wrote:

    > ...
    > 0D0A to the file.
    >
    > Is this a bug or expected behavior?
    > It sure took me by surprise.


    You're running Windows, right? Then, this is expected behaviour, as Windows
    treats text and binary files different, extending \n to \r\n (as that is
    the Windows end of line marker).

    If you want to only write \n to the file, use:

    f = file("t.txt","wb")

    ....

    windows-line-feeds-are-from-hell'ly yours,
    --- Heiko.
     
    Heiko Wundram, Dec 15, 2005
    #2
    1. Advertising

  3. Guest

    Thanks much.

    Yes, I am using Windows.
     
    , Dec 15, 2005
    #3
    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. Dave Tichenor
    Replies:
    3
    Views:
    32,184
    Steven Cheng[MSFT]
    Feb 17, 2004
  2. Andrew
    Replies:
    6
    Views:
    3,589
    Andrew
    Dec 9, 2003
  3. OutlookNewbieDev

    carriage return and line feed characters are lost when passed in p

    OutlookNewbieDev, Oct 28, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    173
    OutlookNewbieDev
    Oct 28, 2005
  4. aa

    line feed and carriage return

    aa, Apr 21, 2004, in forum: ASP General
    Replies:
    2
    Views:
    230
  5. Steve Anderson
    Replies:
    3
    Views:
    265
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page