Python Tutorials

Python File Handling : Read, Write & Append Text Files

Up until this point, every piece of data you have created in your Python programs—every list, dictionary, and variable—has been stored in your computer’s short-term RAM. This means the moment your Python script finishes running or your computer restarts, all of that data vanishes into thin air.

If you are building a game and need to save a player’s high score, or if you are scraping data from the web and need to analyze it later, you must store that data permanently on your hard drive.

This process is known as File Handling (or File I/O). Learning how to safely read data from text files, write new data into them, and append continuous streams of logs is what transforms a temporary script into a permanent, persistent software application.

Reading and Writing Text Files

In Python, File Handling is the process of interacting with files on your computer’s storage system. This is accomplished using the built-in open() function, which establishes a connection between your Python script and the physical file.

When you open a file, you must specify a File Mode that tells Python your exact intentions:

  • 'r' (Read): Opens a file for reading only. (This is the default mode).
  • 'w' (Write): Opens a file for writing. Warning: If the file already exists, this mode will instantly erase and overwrite everything inside it! If the file does not exist, it creates a new one.
  • 'a' (Append): Opens a file for writing, but instead of overwriting, it adds new data to the very end of the file.
  • 'r+' (Read & Write): Opens the file to both read the contents and write new data.

Syntax & Basic Usage

The most fundamental way to interact with a file is to open() it, perform an action (like reading), and then explicitly close() it so the computer can free up memory.

(Note: We will learn a much safer, more modern way to do this in the next section!)

# 1. Open the file in 'w' (Write) mode. (Creates the file if it doesn't exist)
# We also specify encoding="utf-8" to ensure special characters save correctly.
my_file = open("greeting.txt", "w", encoding="utf-8")

# 2. Write data into the file
my_file.write("Hello, Python Developer!")

# 3. CRITICAL: Always close the file to save changes and free memory
my_file.close()

print("File created successfully.")

# Expected Output:
# File created successfully.
# (A new physical file named 'greeting.txt' now exists on your computer)

Code language: PHP (php)

Python File Handling Methods and open() Arguments

Managing files manually with .close() is dangerous because if your program crashes halfway through, the file might remain locked open, corrupting your data.

To solve this, professional Python developers universally use the Context Manager (the with statement). Let’s explore every file operation using this industry-standard technique.

1. The Context Manager (with open())

The with statement automatically handles opening and closing the file for you. As soon as the indented block of code finishes, Python guarantees the file is safely closed, even if the program crashes!

# The 'with' statement creates a safe environment for file handling
# 'as log_file' assigns the opened file object to a variable name
with open("system_log.txt", "w", encoding="utf-8") as log_file:
    log_file.write("System initialized successfully.")

# Once we un-indent, the file is ALREADY closed. No .close() needed!
print("Log written safely.")

# Expected Output:
# Log written safely.

Code language: PHP (php)

2. Writing to a File (.write())

When using 'w' mode, Python starts at the absolute beginning of the file. If you write multiple lines, you must manually include the newline character (\n) to drop down to the next line.

with open("grocery_list.txt", "w", encoding="utf-8") as grocery_file:
    # \n creates a line break
    grocery_file.write("1. Apples\n")
    grocery_file.write("2. Whole Milk\n")
    grocery_file.write("3. Loaf of Bread\n")

print("Groceries saved!")

# Expected Output:
# Groceries saved!
# (The file 'grocery_list.txt' now has 3 distinct lines of text)

Code language: PHP (php)

3. Appending to a File ('a' mode)

If you run the 'w' script above a second time, your old grocery list will be instantly deleted and replaced. To add new data without destroying the old data, use the 'a' (Append) mode.

# Opening the existing file in 'a' (Append) mode
with open("grocery_list.txt", "a", encoding="utf-8") as grocery_file:
    # This gets added to the absolute bottom of the file
    grocery_file.write("4. Coffee Beans\n")

print("Item appended to existing list!")

# Expected Output:
# Item appended to existing list!
# (The file now contains 4 items. The original 3 were preserved.)

Code language: PHP (php)

4. Reading an Entire File (.read())

To retrieve data, open the file in 'r' (Read) mode. The .read() method grabs the entire contents of the text file and stores it as one massive String variable.

# Open in 'r' mode (default)
with open("grocery_list.txt", "r", encoding="utf-8") as grocery_file:
    # Read everything at once
    entire_list_contents = grocery_file.read()

print("--- FILE CONTENTS ---")
print(entire_list_contents)

# Expected Output:
# --- FILE CONTENTS ---
# 1. Apples
# 2. Whole Milk
# 3. Loaf of Bread
# 4. Coffee Beans

Code language: PHP (php)

5. Reading Line by Line (.readline() and .readlines())

Reading a massive 5-gigabyte server log all at once using .read() will crash your computer’s RAM. Instead, you should read large files line by line.

  • .readline(): Reads exactly one single line and pauses.
  • .readlines(): Reads the entire file and automatically splits it into a List of strings, where every line is a separate item in the list.
with open("grocery_list.txt", "r", encoding="utf-8") as grocery_file:
    # Returns a Python List containing every line
    lines_array = grocery_file.readlines()

print("First item in the list:", lines_array[0].strip())
print("Total items:", len(lines_array))

# Expected Output:
# First item in the list: 1. Apples
# Total items: 4

Code language: PHP (php)

(Note: We used .strip() to remove the invisible \n newline character from the end of the printed string.)

6. The Most Pythonic Way to Read a File

Instead of using .readlines(), the most memory-efficient and professional way to read a file line-by-line is to use a simple for loop directly on the file object itself!

with open("grocery_list.txt", "r", encoding="utf-8") as grocery_file:
    # Python automatically reads one line at a time, saving memory!
    for line in grocery_file:
        # We strip() the newline character to prevent double-spacing in the print output
        clean_line = line.strip()
        print(f"Reading: {clean_line}")

# Expected Output:
# Reading: 1. Apples
# Reading: 2. Whole Milk
# Reading: 3. Loaf of Bread
# Reading: 4. Coffee Beans

Code language: PHP (php)

7. Cursor Control (.seek() and .tell())

When Python reads a file, an invisible “cursor” moves through the text, exactly like when you are typing.

  • .tell() tells you exactly what byte position the cursor is currently at.
  • .seek(byte_position) forces the cursor to jump to a specific location in the file.
with open("grocery_list.txt", "r", encoding="utf-8") as grocery_file:
    print(f"Starting cursor position: {grocery_file.tell()}")
    
    first_line = grocery_file.readline()
    print(f"Read: {first_line.strip()}")
    print(f"Cursor position after one line: {grocery_file.tell()}")
    
    # Force the cursor all the way back to the absolute beginning (Byte 0)
    grocery_file.seek(0)
    print("Cursor reset to byte 0.")
    
    # Re-reading the same line!
    first_line_again = grocery_file.readline()
    print(f"Read again: {first_line_again.strip()}")

# Expected Output:
# Starting cursor position: 0
# Read: 1. Apples
# Cursor position after one line: 11
# Cursor reset to byte 0.
# Read again: 1. Apples

Code language: PHP (php)

Real-World Practical Examples

Scenario 1: A Persistent Event Logger Function

In real-world applications, you need to track errors, user logins, and system events. We can build a reusable function that appends timestamped events to a continuous log file.

from datetime import datetime

def log_system_event(event_message):
    """Appends a timestamped message to the server log."""
    
    # Get current time as a clean string
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    # Format the final string
    log_entry = f"[{current_time}] EVENT: {event_message}\n"
    
    # Open the file in Append ('a') mode
    with open("server_logs.txt", "a", encoding="utf-8") as log_file:
        log_file.write(log_entry)
        
    print(f"Logged: {event_message}")

# Simulating application events
log_system_event("Application booted successfully.")
log_system_event("User 'Admin' logged in.")
log_system_event("Warning: High memory usage detected.")

# Expected Output:
# Logged: Application booted successfully.
# Logged: User 'Admin' logged in.
# Logged: Warning: High memory usage detected.
# (The 'server_logs.txt' file now has 3 permanent, timestamped records!)

Code language: PHP (php)

Scenario 2: Data Extraction and Filtering

Imagine you have a text file containing employee names and their departments, separated by commas. We need to read this file, extract only the “Engineering” employees, and save them to a brand new file.

# Step 1: Let's quickly create the dummy data file first
with open("all_employees.txt", "w", encoding="utf-8") as f:
    f.write("Alice,Engineering\nBob,Sales\nCharlie,Engineering\nDiana,HR\n")

# Step 2: Now, let's extract the data
engineers_list = []

# Open the original file to READ
with open("all_employees.txt", "r", encoding="utf-8") as source_file:
    for line in source_file:
        # Split the string by the comma into a list: ['Alice', 'Engineering']
        employee_data = line.strip().split(",")
        name = employee_data[0]
        department = employee_data[1]
        
        if department == "Engineering":
            engineers_list.append(name)

# Step 3: Write the filtered data to a NEW file
with open("engineering_team.txt", "w", encoding="utf-8") as target_file:
    for engineer in engineers_list:
        target_file.write(f"Engineer: {engineer}\n")

print(f"Successfully extracted {len(engineers_list)} engineers to a new file.")

# Expected Output:
# Successfully extracted 2 engineers to a new file.
# (A new file 'engineering_team.txt' is created containing Alice and Charlie)

Code language: PHP (php)

Best Practices & Common Pitfalls

  • The 'w' Mode Overwrite Trap: The most devastating mistake beginners make is opening an existing file in 'w' (Write) mode when they meant to use 'a' (Append). Opening an existing file in 'w' mode instantly permanently deletes all data inside it before you even write a single line of code.
  • Always use Context Managers (with): Never rely on .close(). If an error occurs during file writing, your program will crash before reaching .close(), leaving the file locked and potentially corrupting the data. The with statement guarantees safe closure.
  • Always Specify Character Encoding: Always include encoding="utf-8" inside your open() function. If you don’t, Python will default to whatever your operating system uses. A file created on a Mac might suddenly show strange, broken characters (like é) when opened on a Windows computer. UTF-8 ensures global compatibility.
  • Missing Newlines: .write() does not automatically hit the “Enter” key for you. If you write “Apple” and then write “Banana”, your text file will say “AppleBanana”. Always append \n to your strings when writing multiple lines.

Summary

  • File Handling allows Python to persistently save and retrieve data from your computer’s hard drive.
  • Use the open() function combined with the with statement (Context Manager) for safe, automatic file closure.
  • File Modes: Use 'r' to read data, 'w' to create/overwrite a file, and 'a' to append data to the end of a file.
  • Use .read() to load an entire file into a string, or iterate directly over the file object with a for loop to read it memory-efficiently line-by-line.
  • Remember that writing to files requires you to manually include the \n character to create line breaks.

Leave a Comment