Appending data to a file

+1 vote
How do I append to a file in python? I don't wish to overwrite the file, just append some content.
Jul 20, 2018 in Python by kaalabilli
• 1,090 points
24 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+1 vote
Best answer
First open the file that you want to append in append(a) mode and then write into it

Ex:
with open("example.txt", "rb") as myfile:
    myfile.read()

'This is the text that already exists'

with open("example.txt", "a") as myfile:
    myfile.write("\nThis text will be append to the file");

with open("example.txt", "rb") as myfile:
    myfile.read()

'This is the text that already exists
This text will be append to the file'
answered Jul 23, 2018 by Omkar
• 66,880 points

selected Aug 8, 2018 by Priyaj
0 votes

Try this 

open("test.txt", "a") as myfile:
    myfile.write("appended text")

answered Aug 8, 2018 by Priyaj
• 56,140 points

Related Questions In Python

0 votes
1 answer

Appending data to a file

with open("test.txt", "a") as myfile: ...READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
22 views
0 votes
1 answer

How to write data to a file in Python?

Refer to the below code. data=’whatever your data ...READ MORE

answered May 12 in Python by Shaam
10 views
0 votes
1 answer

How to read data from a text file using Python?

Refer to the below example where the ...READ MORE

answered May 12 in Python by Sushma
12 views
+1 vote
4 answers

How to write nested dictionaries to a CSV file

Using DictWriter there is no need in ...READ MORE

answered Oct 18, 2018 in Python by Richard William
2,951 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
55 views
+1 vote
1 answer

Appending data to a file

To append a file without overwriting, open ...READ MORE

answered Aug 7, 2018 in Python by Omkar
• 66,880 points

edited Aug 7, 2018 by Omkar 26 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2 in Python by Omkar
• 66,880 points
13 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.