Writelines() a bit confusing
Gre7g Luterman
hafeliel at yahoo.com
Sat May 19 09:46:05 EDT 2007
"aiwarrior" <zubeido at yahoo.com.br> wrote in message
news:1179578195.565952.244110 at u30g2000hsc.googlegroups.com...
> If file.WriteLines( seq ) accepts a list and it says it writes lines,
> why does it write the whole list in a single line. Be cause of that
> the reverse of file.writelines(seq) is not file.readlines().
> Are the assumptions i made correct? If yes why is this so?
readlines() and writelines() are complimentary. readlines() leaves the line
terminators intact. It does not strip them off. writelines() does not add in
carriage returns for the same reason. For example:
>>> D=open("temp.txt").readlines()
>>> D
['the quick\n', 'brown fox\n', 'jumps over\n', 'the lazy dog.']
>>> open("temp2.txt","w").writelines(D)
will create temp2.txt to be identical to temp.txt.
More information about the Python-list
mailing list