View Discussion Nội dung chính
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we are going to discuss various approaches to keep old content while writing files in Python.
We can keep old content while using write in python by opening the file in append mode. To open a file in append mode, we can use either ‘a‘ or ‘a+‘ as the access mode. The definition of these access modes are as follows:
- Append Only [‘a’]: Open the file for writing. The file is made if it doesn’t exist. The handle is positioned at the top of the file. The data being written are going to be inserted at the top, after the prevailing data.
- Append with Read [‘a+’]: Open the file for reading and writing. The file is made if it doesn’t exist. The handle is positioned at the top of the file. The data being written are going to be inserted at the top, after the prevailing data.
Approach:
- We will first open the file in the append mode i.e. make use of either ‘a’ or ‘a+’ access mode to open a file.
- Now, we will simply add the content at the bottom of the file thus keeping the old content of the file.
- Then, we will close the opened file in the program.
We are going to use the below text file to perform all the approaches:
Below is the complete implementation of the approach explained above:
Example1: Adding new content to the file while keeping the old content with ‘a’ as an access mode.
Python3
file
=
open
[
"gfg input file.txt"
,
"a"
]
content
=
"\n\n# This Content is added through the program #"
file
.write[content]
file
.close[]
Output:
Example 2: Adding new content to the file while keeping the old content with ‘a+’ as an access mode.
Python3
file
=
open
[
"gfg input file.txt"
,
"a+"
]
file
.seek[
0
]
content
=
file
.read[]
print
[content]
content
=
"\n\n# This Content is added through the program #"
file
.write[content]
file
.close[]
Output:
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes etc.
I've been having trouble with this for a while. How do I open a file in python and continue writing to it but not overwriting what I had written before?
For instance:
The code below will write 'output is OK'. Then the next few lines will overwrite it and it will just be 'DONE'
But I want both 'output is OK' 'DONE' in the file
f = open['out.log', 'w+']
f.write['output is ']
# some work
s = 'OK.'
f.write[s]
f.write['\n']
f.flush[]
f.close[]
# some other work
f = open['out.log', 'w+']
f.write['done\n']
f.flush[]
f.close[]
I want to be able to freely open and write to it in intervals. Close it. Then repeat the process over and over.
Thanks for any help :D
asked Nov 16, 2012 at 7:54
user1431282user1431282
6,31513 gold badges49 silver badges67 bronze badges
Open the file in append mode. It will be created if it does not exist and it will be opened at its end for further writing if it does exist:
with open['out.log', 'a'] as f:
f.write['output is ']
# some work
s = 'OK.'
f.write[s]
f.write['\n']
# some other work
with open['out.log', 'a'] as f:
f.write['done\n']
answered Nov 16, 2012 at 7:57
0
Just pass 'a' as argument when you open the file to append content in it. See the doc
f = open['out.log', 'a']
answered Nov 16, 2012 at 7:57
Raunak AgarwalRaunak Agarwal
7,0076 gold badges37 silver badges61 bronze badges
You need to open the file in append mode the second time:
f = open['out.log', 'a']
because every time you open the file in the write mode, the contents of the file get wiped out.
answered Nov 16, 2012 at 7:58
codaddictcodaddict
434k80 gold badges487 silver badges524 bronze badges
After the first writting, you need to use
f = open['out.log', 'a']
to append the text to the content of your file.
answered Nov 16, 2012 at 7:58
with open["test.txt", "a"] as myfile:
myfile.write["appended text"]
answered Nov 16, 2012 at 7:59
ymnymn
2,1652 gold badges21 silver badges39 bronze badges
How do you continue to write in a file in Python?
We can keep old content while using write in python by opening the file in append mode. To open a file in append mode, we can use either 'a' or 'a+' as the access mode. The definition of these access modes are as follows: Append Only ['a']: Open the file for writing.
How do you write to a file multiple times in Python?
write in multiple files python.
fn = open["path of input file.txt","r"].
fnew = fn. read[].
fs = fnew. split['\n'].
for value in fs:.
f = [open["path of output file to write.txt" %i,'w'] for i in range[len[list_of_files]]].
f. write[value].
f. close[].
How do you write to a file in Python?
Steps for writing to text files To write to a text file in Python, you follow these steps: First, open the text file for writing [or append] using the open[] function. Second, write to the text file using the write[] or writelines[] method. Third, close the file using the close[] method.
How do I save a file after writing in Python?
Saving a Text File in Python After learning about opening a File in Python, let's see the ways to save it. Opening a new file in write mode will create a file and after closing the file, the files get saved automatically.