Module 13 · File Handling — topic 5 of 15
Appending
Adding data to the end of a file with mode "a": keeping existing content, creating files that don't exist, building simple logs, and the difference between appending and writing.
Learning objectives
- Append to files with mode "a"
- Compare appending with writing
- Build a simple log file
Definition
Appending writes new data at the end of a file without removing what is already there. Mode "a" creates the file if it doesn't exist; every write goes to the end.
Introduction
A diary gets a new entry every day; you never rewrite the old pages. Appending is how programs add entries to logs, histories and journals.
Why it matters
Using "a" instead of "w" for logs and histories is the difference between keeping a record and losing it on every run.
Detailed explanation
open(path, "a", encoding="utf-8") opens for appending; the file is created if missing.
All writes go to the end of the file, regardless of any seek, so existing content is never overwritten.
Each run of a program can add to the same file, making "a" perfect for logs and audit trails.
Appending many times by reopening the file is simple and safe for occasional writes; for very frequent writes keep the file open or use the logging module.
Mode "a+" allows reading as well; reading starts wherever the position is, so seek(0) first.
Remember to include line endings, as with any write.
Because appended files only grow, programs that read them usually process the most recent lines (for example the last 100) or summarise the whole file, rather than loading everything for every request.
Syntax
- "a" never erases content.
- Creates the file if needed.
- Writes always go to the end.
Code examples
Appending entries
['login', 'view page', 'logout']
Append versus write
['one', 'two'] ['three']
More examples
A simple logger
4 09:10 [ERROR] disk full 2
Step-by-step explanation
- Every call to log opens the file in append mode, adds one line and closes it.
- Earlier entries are preserved, so the file grows with each event.
- The time is passed in so the example output is predictable; real code would use datetime.now().
Expected output
Running “A simple logger” prints exactly:
Practical applications
- Application and web-server logs.
- Recording sensor readings or sales over time.
Best practices
- Use "a" for logs and histories.
- Include timestamps and newlines in each entry.
- Use the logging module for serious applications.
Common mistakes
Logging with "w"
Avoid
Prefer
"w" keeps only the latest entry.
Important notes
- Log files can grow forever; real systems rotate them (logging.handlers.RotatingFileHandler).
Key takeaways
- "a" adds to the end and keeps existing content.
- It creates the file if needed.
- Ideal for logs and records.
Try it yourself
- 1Write a guest-book program that appends each name with a visit number.
- 2Append daily temperatures to a file and then print the average of all entries.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?