Python Fast I/o

Chris Angelico rosuav at gmail.com
Tue Jan 14 09:03:08 EST 2014


On Wed, Jan 15, 2014 at 12:50 AM, Ayushi Dalmia
<ayushidalmia2604 at gmail.com> 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()

with open('index.txt','w') as f:
    for hunk in data:
        f.write(hunk)

You don't need to f.close() - that's what the 'with' block guarantees.
Iterating over data and writing each block separately means you don't
have to first build up a 200MB string. After that, your performance is
going to be mainly tied to the speed of your disk, not anything that
Python can affect.

ChrisA



More information about the Python-list mailing list