Python Fast I/o

Tim Chase python.list at tim.thechases.com
Tue Jan 14 09:18:33 EST 2014


On 2014-01-14 05:50, Ayushi Dalmia wrote:
> I need to write into a file for a project which will be evaluated
> on the basis of time. What is the fastest way to write 200 Mb of
> data, accumulated as a list into a file.
> 
> Presently I am using this:
> 
> with open('index.txt','w') as f:
>       f.write("".join(data))
>       f.close()
> 
> where data is a list of strings which I want to dump into the
> index.txt file -- 

Most file-like objects should support a writelines() method which
takes an iterable and should save you the trouble of joining all the
content (and as Chris noted, you don't need the .close() since the
with handles it) so the whole thing would condense to:

  with open('index.txt', 'w') as f:
    f.writelines(data)

-tkc





More information about the Python-list mailing list