How to write fast into a file in python?

lokeshkoppaka at gmail.com lokeshkoppaka at gmail.com
Thu May 16 23:20:26 EDT 2013



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.



More information about the Python-list mailing list