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

J

jcb

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?
 
H

Heiko Wundram

...
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top