Most efficient way to write data out to a text file?

Bengt Richter bokr at oz.net
Thu Jun 27 10:44:21 EDT 2002


On Thu, 27 Jun 2002 15:16:46 +0300, Christos "TZOTZIOY" Georgiou <DLNXPEGFQVEB at spammotel.com> wrote:

>On 27 Jun 2002 02:01:35 GMT, rumours say that bokr at oz.net (Bengt
>Richter) might have written:
>
>>Try accumulating your chunks in a list,
>>like sl=[] followed by sl.append('chunk') instead, and then do f.write(''.join(sl))
>>instead of the f.write(s) you would have done.
>
>A minor correction, if I may add:
>
>f.writelines(sl)
>might be quite more efficient than
>f.write(''.join(sl))
>
>AFAIK the output is the same, as long as one remembers to insert the
>necessary '\n' for line breaks where needed :)

Thank you for pointing that out. (That should teach me to throw phrases around
like "typical newbie performance killer." ;-) I have not been in the habit of
using writelines, and did not remember that since readlines preserves newlines,
writelines would presumably, for symmetry, not add them.

I expect to be using writelines more in the future ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list