How to write fast into a file in python?

Dave Angel davea at davea.name
Fri May 17 07:58:43 EDT 2013


On 05/17/2013 12:35 AM, lokeshkoppaka at gmail.com wrote:
> On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh... at gmail.com wrote:
>> I need to write numbers into a file upto 50mb and it should be fast
>>
>> can any one help me how to do that?
>>
>> i had written the following code..
>>
>>  <SNIP>
>> value = 0
>>
>> with open(filename, "w") as f:
>>
>> while f.tell()< size:
>>
>> f.write("{0}\n".format(value))
>>   <SNIP more double-spaced nonsense from googlegroups>
   If you must use googlegroups, at least read this
   http://wiki.python.org/moin/GoogleGroupsPython.
>>
>>
>> it takes about 20sec i need 5 to 10 times less than that.
> size = 50mb
>

Most of the time is spent figuring out whether the file has reached its 
limit size.  If you want Python to go fast, just specify the data.  On 
my Linux system, it takes 11 seconds to write the first 6338888 values, 
which is just under 50mb.  If I write the obvious loop, writing that 
many values takes .25 seconds.

-- 
DaveA



More information about the Python-list mailing list