How to write fast into a file in python?

Carlos Nepomuceno carlosnepomuceno at outlook.com
Sat May 18 13:00:06 EDT 2013


Python really writes '\n\r' on Windows. Just check the files.

Internal representations only keep '\n' for simplicity, but if you wanna keep track of the file length you have to take that into account. ;)

________________________________
> Date: Sat, 18 May 2013 08:49:55 +0100 
> Subject: RE: How to write fast into a file in python? 
> From: fabiosantosart at gmail.com 
> To: carlosnepomuceno at outlook.com 
> CC: python-list at python.org 
>  
>  
> On 17 May 2013 19:38, "Carlos Nepomuceno"  
> <carlosnepomuceno at outlook.com<mailto:carlosnepomuceno at outlook.com>>  
> wrote: 
> > 
> > Think the following update will make the code more portable: 
> > 
> > x += len(line)+len(os.linesep)-1 
> > 
> > Not sure if it's the fastest way to achieve that. :/ 
> > 
>  
> 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. 		 	   		  


More information about the Python-list mailing list