How to write fast into a file in python?

Carlos Nepomuceno carlosnepomuceno at outlook.com
Fri May 17 14:33:23 EDT 2013


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

> On Fri, 17 May 2013 18:20:33 +0300, Carlos Nepomuceno wrote:
>
>> ### fastwrite5.py ###
>> import cStringIO
>> size = 50*1024*1024
>> value = 0
>> filename = 'fastwrite5.dat'
>> x = 0
>> b = cStringIO.StringIO()
>> while x < size:
>>     line = '{0}\n'.format(value)
>>     b.write(line)
>>     value += 1
>>     x += len(line)+1
>
> Oh, I forgot to mention: you have a bug in this function. You're already
> including the newline in the len(line), so there is no need to add one.
> The result is that you only generate 44MB instead of 50MB. 		 	   		  


More information about the Python-list mailing list