Skip to main content

/docs/images/banner.png

Python

Append to file

Append content to file and print the contents of the file

# Append content to file file.txt and print the contents of the file.
content = 'hello'

with open('file.txt', 'a') as file:
file.write(content)

with open('file.txt', 'r') as file:
print(file.read())

# Output:
# og file content
# hello

note

To compile and run the program, you can use the following commands:

python3 foo.py
note

file file.txt should be in the same directory as the script.