How to use writelines to append new lines to an existing file

Nico Grubert nicogrubert at
Thu Sep 22 14:52:06 CEST 2005

Hi there,

I would like to open an existing file that contains some lines of text 
in order to append a new line at the end of the content.

My first try was:

 >>> f = open('/tmp/myfile', 'w') #create new file for writing
 >>> f.writelines('123')	     #write first line
 >>> f.close()
 >>> f = open('/tmp/myfile', 'w') #open existing file to append new line
 >>> f.writelines('456')
 >>> f.close()
 >>> f = open('/tmp/myfile', 'r') # open file for reading

I supposed to have:

Does  f = open('/tmp/myfile', 'w')  overwrite the existing file or does 
f.writelines('456') replace the first line in the existing file?


More information about the Python-list mailing list