This tutorial will guide you through Python’s file handling capabilities, from basic file operations to advanced techniques. You’ll learn how to read, write, and manipulate files, handle exceptions, and work with different file formats. By the end, you’ll be equipped to handle real-world file processing tasks with confidence.

Beginner30 minutes

Step 1: Step 1: Opening and Closing Files

Learn how to open and close files in Python using the open() function and the with statement. Understand the importance of properly closing files to avoid resource leaks.

# Opening a file in read mode
file = open('example.txt', 'r')
content = file.read()
file.close()

# Using 'with' statement for automatic closing
with open('example.txt', 'r') as file:
    content = file.read()

Practice Exercise

Write a Python script that opens a file named 'data.txt', reads its content, and prints the number of lines in the file. Use the with statement to ensure the file is properly closed.

Show Solution
with open('data.txt', 'r') as file:
    lines = file.readlines()
    print(f'Number of lines: {len(lines)}')

Step 2: Step 2: Reading from Files

Explore different methods for reading file content, including read(), readline(), and readlines(). Understand when to use each method based on your needs.

# Reading the entire file
with open('example.txt', 'r') as file:
    content = file.read()

# Reading line by line
with open('example.txt', 'r') as file:
    for line in file:
        print(line)

# Reading all lines into a list
with open('example.txt', 'r') as file:
    lines = file.readlines()

Practice Exercise

Create a script that reads a file named 'log.txt' and prints only the lines that contain the word 'ERROR'. Use the readlines() method.

Show Solution
with open('log.txt', 'r') as file:
    lines = file.readlines()
    for line in lines:
        if 'ERROR' in line:
            print(line)

Step 3: Step 3: Writing to Files

Learn how to write data to files using write() and writelines(). Understand the difference between writing in 'w' (write) mode and 'a' (append) mode.

# Writing to a file
with open('output.txt', 'w') as file:
    file.write('Hello, World!')

# Appending to a file
with open('output.txt', 'a') as file:
    file.write('\nAppending a new line.')

Practice Exercise

Write a Python script that takes user input and appends it to a file named 'notes.txt'. Ensure the script can handle multiple lines of input.

Show Solution
with open('notes.txt', 'a') as file:
    while True:
        user_input = input('Enter a note (or type exit to quit): ')
        if user_input.lower() == 'exit':
            break
        file.write(user_input + '\n')

Step 4: Step 4: Handling File Exceptions

Understand how to handle exceptions when working with files, such as FileNotFoundError and PermissionError. Learn to use try-except blocks to make your file handling code more robust.

try:
    with open('nonexistent.txt', 'r') as file:
        content = file.read()
except FileNotFoundError:
    print('File not found!')
except PermissionError:
    print('Permission denied!')

Practice Exercise

Write a script that attempts to open a file named 'config.txt'. If the file does not exist, create it and write 'DEFAULT_CONFIG' into it. Handle any potential exceptions.

Show Solution
try:
    with open('config.txt', 'r') as file:
        content = file.read()
except FileNotFoundError:
    with open('config.txt', 'w') as file:
        file.write('DEFAULT_CONFIG')

Step 5: Step 5: Working with Binary Files

Learn how to read and write binary files using 'rb' and 'wb' modes. Understand the use cases for binary file handling, such as working with images or serialized data.

# Reading a binary file
with open('image.png', 'rb') as file:
    binary_data = file.read()

# Writing to a binary file
with open('copy.png', 'wb') as file:
    file.write(binary_data)

Practice Exercise

Create a script that reads a binary file named 'data.bin' and writes its content to a new file named 'backup.bin'. Ensure the script handles large files efficiently.

Show Solution
with open('data.bin', 'rb') as source_file, open('backup.bin', 'wb') as dest_file:
    for chunk in iter(lambda: source_file.read(1024), b''):
        dest_file.write(chunk)

Step 6: Step 6: Working with CSV Files

Learn how to read and write CSV files using Python's csv module. Understand how to handle headers and work with dictionaries for more structured data.

import csv

# Reading a CSV file
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

# Writing to a CSV file
with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', 30])

Practice Exercise

Write a script that reads a CSV file named 'students.csv' and calculates the average age of the students. Assume the CSV has a header row with columns 'Name' and 'Age'.

Show Solution
import csv

ages = []
with open('students.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        ages.append(int(row['Age']))

average_age = sum(ages) / len(ages)
print(f'Average age: {average_age}')

Step 7: Step 7: Working with JSON Files

Learn how to read and write JSON files using Python's json module. Understand how to serialize and deserialize Python objects to and from JSON format.

import json

# Writing to a JSON file
data = {'name': 'Alice', 'age': 30}
with open('data.json', 'w') as file:
    json.dump(data, file)

# Reading from a JSON file
with open('data.json', 'r') as file:
    data = json.load(file)
    print(data)

Practice Exercise

Create a script that reads a JSON file named 'config.json' and updates a specific key-value pair. Write the updated data back to the file.

Show Solution
import json

with open('config.json', 'r') as file:
    config = json.load(file)

config['version'] = '2.0'

with open('config.json', 'w') as file:
    json.dump(config, file, indent=4)

Step 8: Step 8: Advanced File Handling Techniques

Explore advanced file handling techniques, such as working with temporary files, using os and shutil modules for file operations, and handling large files efficiently.

import tempfile
import os

# Creating a temporary file
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
    temp_file.write(b'Hello, World!')
    temp_file_path = temp_file.name

# Using shutil to copy files
import shutil
shutil.copy(temp_file_path, 'backup.txt')

# Deleting the temporary file
os.unlink(temp_file_path)

Practice Exercise

Write a script that creates a temporary file, writes some data to it, copies it to a new location, and then deletes the temporary file. Use the tempfile, shutil, and os modules.

Show Solution
import tempfile
import shutil
import os

with tempfile.NamedTemporaryFile(delete=False) as temp_file:
    temp_file.write(b'This is temporary data.')
    temp_file_path = temp_file.name

shutil.copy(temp_file_path, 'backup.txt')
os.unlink(temp_file_path)

Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.

Sign in

Click to access the login or register cheese