Editor

Python Program to Append Text to a File

Beginner · File handling

Appending adds new text to the end of a file and keeps what was already there. Opening a file in "a" (append) mode does this, while "w" mode would erase the file first. This program makes a small diary file, appends two new entries, and shows the file before and after.

Program

append_to_file.py
# Make a sample file to work on
with open("diary.txt", "w") as f:
f.write("Monday: Learned about lists\n")
f.write("Tuesday: Practised loops\n")
with open("diary.txt", "r") as f:
print("Before appending:")
print(f.read())
new_entry = "Wednesday: Wrote my first file program" # or: new_entry = input("Today's entry: ")
with open("diary.txt", "a") as f: # "a" writes at the end and keeps the old text
f.write(new_entry + "\n")
f.write("Thursday: Learned the file modes\n")
with open("diary.txt", "r") as f:
print("After appending:")
print(f.read(), end="")
Output
Before appending:
Monday: Learned about lists
Tuesday: Practised loops

After appending:
Monday: Learned about lists
Tuesday: Practised loops
Wednesday: Wrote my first file program
Thursday: Learned the file modes

How it works

  1. The program first writes two lines in "w" mode, so there is a file to append to; "w" also empties it, so every run starts from the same two lines.
  2. Opening the same file with "a" puts the writing position at the end of the file, so the old lines are kept.
  3. Each new entry ends with "\n", so the next entry starts on a line of its own.
  4. If the file does not exist yet, "a" creates it, just like "w" does.

Other ways to write it

The difference between "w" and "a"

The difference between "w" and "a"
for mode in ["w", "a"]:
# Make a sample file to work on
with open("demo.txt", "w") as f:
f.write("old line\n")
with open("demo.txt", mode) as f:
f.write("new line\n")
with open("demo.txt", "r") as f:
print(f'Mode "{mode}":', f.read().splitlines())
Output
Mode "w": ['new line']
Mode "a": ['old line', 'new line']

Both runs start from the same one-line file. Mode "w" wipes the old line before writing, while mode "a" keeps it and adds the new line after it.

Appending with a function

Appending with a function
def add_line(filename, text):
with open(filename, "a") as f:
f.write(text + "\n")
# Make a sample file to work on
with open("shopping.txt", "w") as f:
f.write("milk\n")
for item in ["bread", "eggs", "rice"]:
add_line("shopping.txt", item)
with open("shopping.txt", "r") as f:
print("Shopping list:", f.read().split())
Output
Shopping list: ['milk', 'bread', 'eggs', 'rice']

Each call opens the file in append mode, adds one line and closes it again, which is how log files grow one entry at a time.

Try it yourself

Append the lines "Item 1" to "Item 5" in a loop, then print the file to check all five were added.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises