Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 15th, 2005, 12:15 AM
jcb@iteris.com
Guest
 
Posts: n/a
Default bug? file.write('\n') produces a carriage return and a line feed

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?

  #2  
Old December 15th, 2005, 12:25 AM
Heiko Wundram
Guest
 
Posts: n/a
Default Re: bug? file.write('\n') produces a carriage return and a line feed

jcb@iteris.com wrote:
[color=blue]
> ...
> 0D0A to the file.
>
> Is this a bug or expected behavior?
> It sure took me by surprise.[/color]

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.
  #3  
Old December 15th, 2005, 12:35 AM
jcb@iteris.com
Guest
 
Posts: n/a
Default Re: bug? file.write('\n') produces a carriage return and a line feed

Thanks much.

Yes, I am using Windows.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles