473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Sep 22 '05 #1
2 25156
Use
file = open(open('/tmp/myfile', 'a')) the second time
when you want to append line

"Nico Grubert" <ni*********@gmail.com> wrote in message
news:ma************************************@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

Sep 22 '05 #2
Nico Grubert <ni*********@gmail.com> wrote in
news:ma************************************@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


There is a good explanation in the tutorial:
http://docs.python.org/tut/node9.htm...00000000000000

and more detail is available in the docs for builtin functions:
http://docs.python.org/lib/built-in-funcs.html
(look under file() but probably still use open())

That said, open(file, 'a') will open an existing file to append.

max
Sep 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: | last post by:
Use the method InsertAfter() from the XmlDocument class or AppendChild from XmlNode class. Here is a short example to use InsertAfter(). http://weblogs.asp.net/sonukapoor/articles/132854.aspx ...
4
by: Shane | last post by:
Please give me suggestions on how to append a pdf file to another pdf file in C++.
2
by: Roy Gourgi | last post by:
Hi, How could I append a text file to another text file. Let's say that I have a File1 and I want to append File2 at the end of File1, how would I do that? TIA Roy
5
by: Simone M | last post by:
Here is my problem: I need to append a string to a file every time the user downloads this file. The string is different for every user. I would like to append the string to the file via ftp. But...
1
by: nasirmajor | last post by:
Dear all i have the following code for writing to the existing file. =========================== void AppendToFileEng() { SqlDataReader gr = null; string engpath2=""; gr =...
6
by: shalakasan | last post by:
Hi, I want to open a binary file and write to a specific location in File. Here is how my code looks like: int main() { long lbuf = 0; int lreadBytes = 0;
2
by: Kevien Lee | last post by:
Hi , I had a strang problam ,when i use StreamWriter to append to a file,i found that if i don't close the StreamReader it couldn't write the data into file,the code as folllow that: class...
1
by: mamin | last post by:
Hi, I need to output result of my query to txt file. So I'm using -o parameter, for example: osql.exe -s (local) -d database1 -U sa -P sa -i 'c:\\queryFile.sql' -o 'c:\\output.txt' But it...
1
by: harishmitty | last post by:
Lemme explain exactly what am doing. Its pretty simple. String^ file_name = "c:\\test.txt"; StreamWriter^ sw = gcnew StreamWriter(file_name); sw->WriteLine("Hello"); When I do this, everytime...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.