How to write fast into a file in python?

lokeshkoppaka at gmail.com lokeshkoppaka at gmail.com
Fri May 17 00:35:15 EDT 2013


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..
> 
> -----------------------------------------------------------------------------------------------------------
> 
> def create_file_numbers_old(filename, size):
> 
> start = time.clock()
> 
> 
> 
> value = 0
> 
> with open(filename, "w") as f:
> 
> while f.tell()< size:
> 
> f.write("{0}\n".format(value))
> 
> value += 1
> 
> 
> 
> end = time.clock()
> 
> 
> 
> print "time taken to write a file of size", size, " is ", (end -start), "seconds \n"
> 
> ------------------------------------------------------------------------------------------------------------------
> 
> it takes about 20sec i need 5 to 10 times less than that.
size = 50mb



More information about the Python-list mailing list