How do you append to a file with Python?

PythonServer Side ProgrammingProgramming

To append to a file, you must open the file in append mode by specifying 'a' as the mode(a=append) when opening it. For example,

f = open('my_file.txt', 'a')
file_content = f.read()
f.write('Hello World')
f.close()

Above code opens my_file.txt in append mode and appends the file to contain "Hello World" at the end.

raja
Published on 22-Dec-2017 10:55:23
Advertisements