Append Files

Python · File Handling

Theory

Mode 'a' (append) opens a file for writing but keeps everything already in it: every write goes to the end of the file. If the file does not exist yet, it is created, just like 'w'. The difference is that 'w' starts from an empty file each time, while 'a' adds on.

This makes append the natural choice for logs and audit trails. Each run of with open('log.txt', 'a') as f: f.write('Login Ravi\n') adds one new line after the previous ones, so the history builds up over time. As with write mode, you must add '\n' yourself or entries will run together on one line. In append mode the write position is always the end, even if you move it with seek(); 'a+' also lets you read the file.

Why we use it

Some files must grow rather than be replaced - activity logs, daily records, error histories. Append mode adds new entries safely without having to read and rewrite the whole file.

When to use it

Use 'a' to add records to a log or running file. Use 'w' when each run should produce a fresh file. For high-volume logging, Python's logging module handles formatting and file rotation for you.

How it works

  1. open('log.txt', 'a')
  2. Does log.txt exist? Yes: Keep existing lines, position at the end. No: Create an empty file.
  3. f.write('Login Ravi\n')
  4. New text lands after the old content
  5. Close: file now has one more line

Syntax

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

Example

with open('log.txt', 'a') as f:
    f.write('New entry\n')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic