Simple file writing techiques ...

Will McGugan will at willmcgugan.com
Wed Jul 19 08:07:36 EDT 2006


cdecarlo wrote:

>
> fout = open('somefile','w')
> for line in convertedData:
>   fout.write("%s\n" % line)
> fout.close()
>
>  -- or --
>
> fout = open('somefile','w')
> fout.write("%s" % '\n'.join(convertedData))
> fout.close()
>

I'd go for something like...

fout = open('somefile','w')
fout.writelines( line+"\n" for line in convertedData )
fout.close()

Although your first solution should perform about the same.

If you have 2.5, you may prefer this...

with open('somefile','w') as fout:
    fout.writelines( line+"\n" for line in convertedData )


> ... or maybe some hybrid of the two which writes chunks of the
> convertedData list out in one shot ...

The OS should buffer it for you.


Will McGugan




More information about the Python-list mailing list