By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Writelines() a bit confusing

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. something = ['re','ri','ro']
  2. f.writelines( something )
  3. something_else = f.readlines()
  4.  
In this case the list something_else will be diffrent from something

May 19 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
aiwarrior schrieb:
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.

Expand|Select|Wrap|Line Numbers
  1. something = ['re','ri','ro']
  2. f.writelines( something )
  3. something_else = f.readlines()
  4.  
In this case the list something_else will be diffrent from something

Please see this:
>>help(f.writelines)
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.
May 19 '07 #2

P: n/a
aiwarrior wrote:
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.

Expand|Select|Wrap|Line Numbers
  1. something = ['re','ri','ro']
  2. f.writelines( something )
  3. something_else = f.readlines()
  4.  
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 ----------------

May 19 '07 #3

P: n/a
"aiwarrior" <zu*****@yahoo.com.brwrote in message
news:11**********************@u30g2000hsc.googlegr oups.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.
May 19 '07 #4

P: n/a
On May 19, 2:46 pm, "Gre7g Luterman" <hafel...@yahoo.comwrote:
"aiwarrior" <zube...@yahoo.com.brwrote in message

news:11**********************@u30g2000hsc.googlegr oups.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

May 19 '07 #5

P: n/a
En Sat, 19 May 2007 10:31:50 -0300, Stefan Sonnenberg-Carstens
<st***************@pythonmeister.comescribió:
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/pyt...ay/037191.html

--
Gabriel Genellina

May 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.