Writelines() a bit confusing

A

aiwarrior

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?

I find a function called writelines not actualy writing the list in
lines wierd.

Code:
something = ['re','ri','ro']
f.writelines( something )
something_else = f.readlines()
In this case the list something_else will be diffrent from something
 
S

Stefan Sonnenberg-Carstens

aiwarrior said:
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?

I find a function called writelines not actualy writing the list in
lines wierd.

Code:
something = ['re','ri','ro']
f.writelines( something )
something_else = f.readlines()
In this case the list something_else will be diffrent from something

Please see this:
Help on built-in function writelines:

writelines(...)
writelines(sequence_of_strings) -> None. Write the strings to the file.

Note that newlines are not added. The sequence can be any iterable
object
producing strings. This is equivalent to calling write() for each
string.

so you'd want this:

f.writelines([x+os.linesep for x in strings])

or something similar.

Why ? Ask the originator of this function.
One explanation:
If you do this:

f1 = file('file1')
f2 = file('file2','w')
f2.writelines(f1.readlines())
f1.close() ; f2.close()

all is how it should be.
 
S

Steve Holden

aiwarrior said:
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?

I find a function called writelines not actualy writing the list in
lines wierd.

Code:
something = ['re','ri','ro']
f.writelines( something )
something_else = f.readlines()
In this case the list something_else will be diffrent from something
Your list is *not* what you would get in from a call to readlines.

Try it.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
 
G

Gre7g Luterman

aiwarrior said:
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.
 
A

aiwarrior

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:

['the quick\n', 'brown fox\n', 'jumps over\n', 'the lazy dog.']

will create temp2.txt to be identical to temp.txt.

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

Gabriel Genellina

En Sat, 19 May 2007 10:31:50 -0300, Stefan Sonnenberg-Carstens
so you'd want this:

f.writelines([x+os.linesep for x in strings])

or something similar.

You would use os.linesep *only* if the file was opened in binary mode -
unusual if you want to write lines of text.
For a file opened in text mode (the default) the line terminator is always
'\n' - let Python handle the platform differences. On Windows you would
end with malformed or duplicate line terminators if you use explicitely
os.linesep when writing.
See http://mail.python.org/pipermail/python-list/2000-May/037191.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top