A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/332810.html below:

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

How to use writelines to append new lines to an existing file How to use writelines to append new lines to an existing fileGrigoris Tsolakidis gcholakidis at hotmail.com.spam-remove
Thu Sep 22 09:02:45 EDT 2005
Use
file = open(open('/tmp/myfile', 'a')) the second time
when you want to append line

"Nico Grubert" <nicogrubert at gmail.com> wrote in message 
news:mailman.781.1127393534.509.python-list at python.org...
> 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
> >>> f.read()
> '456'
>
> I supposed to have:
> >>> f.read()
> '123\n456\n'
>
>
> Does  f = open('/tmp/myfile', 'w')  overwrite the existing file or does 
> f.writelines('456') replace the first line in the existing file?
>
> Nico 



More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4