write() vs. writelines()

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri May 26 17:51:13 EDT 2006


In <1148669055.390737.57050 at j33g2000cwa.googlegroups.com>, Gregory
Petrosyan wrote:

> Thanks for your reply. I understand this fact, but I wonder why
> writelines() works slowly -- I think C code can be optimised to work
> faster than Python one. Is it correct that writelines(...) is just a
> shorthand for
> 
> for ch in ...:
>     file.write(ch) 
> ?

Depends on `...`.  If it's a string then yes because a string is a
sequence of characters.  But `writelines()` is ment for a sequence of
strings.  It's the counterpart of `readlines()`.  Then it's just a
shorthand for::

  for line in lines:
      file.write(line)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list