Writelines() a bit confusing

aiwarrior zubeido at yahoo.com.br
Sat May 19 12:14:17 EDT 2007


On May 19, 2:46 pm, "Gre7g Luterman" <hafel... at yahoo.com> wrote:
> "aiwarrior" <zube... 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.

I Haven't seen that way before thanks, both of you :D




More information about the Python-list mailing list