tags:

views:

384

answers:

2

The os.write function can be used to writes bytes into a file descriptor (not file object). If I execute os.write(fd, '\n'), only the LF character will be written into the file, even on Windows. I would like to have CRLF in the file on Windows and only LF in Linux. What is the best way to achieve this?

I'm using Python 2.6, but I'm also wondering if Python 3 has a different solution.

+9  A: 

Use this

import os
os.write(fd, os.linesep)
S.Lott
This is misleading; it implies that `os.linesep`, on its own, will do what he wants, when it is merely a string containing the system's newline character(s).
JAB
Given this is the accepted answer I modified it to show an actual line of code.
David Zaslavsky
+5  A: 

How about os.write(<file descriptor>, os.linesep)? (import os is unnecessary because you seem to have already imported it, otherwise you'd be getting errors using os.write to begin with.)

JAB
`os.write` needs the FD as its first argument -- pls fix this answer!
Alex Martelli
Yes, thank you.
JAB