How to write fast into a file in python?

Chris Angelico rosuav at gmail.com
Sat May 18 10:29:50 EDT 2013


On Sat, May 18, 2013 at 5:49 PM, Fábio Santos <fabiosantosart at gmail.com> wrote:
> Putting len(os.linesep)'s value into a local variable will make accessing it
> quite a bit faster. But why would you want to do that?
>
> You mentioned "\n" translating to two lines, but this won't happen. Windows
> will not mess with what you write to your file. It's just that traditionally
> windows and windows programs use \r\n instead of just \n. I think it was for
> compatibility with os/2 or macintosh (I don't remember which), which used \r
> for newlines.
>
> You don't have to follow this convention. If you open a \n-separated file
> with *any* text editor other than notepad, your newlines will be okay.


Into two characters, not two lines, but yes. A file opened in text
mode on Windows will have its lines terminated with two characters.
(And it's old Macs that used to use \r. OS/2 follows the DOS
convention of \r\n, but again, many apps these days are happy with
Unix newlines there too.)

ChrisA



More information about the Python-list mailing list