How we have to append file in python?

Submitted 3 years, 6 months ago
Ticket #225
Views 287
Language/Framework Python
Priority Medium
Status Closed

I have a file and I want to add some lines to the file without overriding the lines that are already present in the file......

Any help will get me out of this problem......

Submitted on Oct 10, 20
sai
add a comment

2 Answers

A short way:

f = open('filename.txt', 'a')
f.write("stuff")
f.close()

More: www.tutorialspoint.com

Submitted 3 years, 6 months ago


Verified

# Open a file with access mode 'a'
file_object = open('sample.txt', 'a')
# Append 'hello' at the end of file
file_object.write('hello')
# Close the file
file_object.close()

Submitted 3 years, 6 months ago


Latest Blogs