File Handling

Reading and Writing Data

Introduction

File handling is an essential skill in programming, allowing you to work with external data sources such as text files, CSV files, and JSON files. In this post, we’ll cover how to read from and write to files in Python, handle common errors, and manage file paths effectively.


1. Basics of File Handling

Python uses the built-in open() function to work with files.

Opening a File

python
Copy code
file = open("example.txt", mode="r")  # Open in read mode

File Modes:

  • r: Read (default)
  • w: Write (overwrites existing content)
  • a: Append (adds content without overwriting)
  • r+: Read and write

Closing a File

Always close files to free resources:

python
Copy code
file.close()

Alternatively, use a with statement to handle files automatically:

python
Copy code
with open("example.txt", mode="r") as file:
    content = file.read()

2. Reading Files

Reading Entire Content

python
Copy code
with open("example.txt", "r") as file:
    content = file.read()
    print(content)

Reading Line by Line

python
Copy code
with open("example.txt", "r") as file:
    for line in file:
        print(line.strip())  # Remove trailing newlines

Reading Specific Number of Characters

python
Copy code
with open("example.txt", "r") as file:
    snippet = file.read(10)  # Read first 10 characters
    print(snippet)

3. Writing Files

Writing New Content

python
Copy code
with open("example.txt", "w") as file:
    file.write("This is a new line.\n")

Appending Content

python
Copy code
with open("example.txt", "a") as file:
    file.write("Adding more content.\n")

4. Working with CSV Files

Python’s csv module makes it easy to read and write CSV files.

Reading CSV Files

python
Copy code
import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Writing CSV Files

python
Copy code
with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Age", "City"])
    writer.writerow(["Alice", 30, "New York"])

5. Working with JSON Files

Python’s json module is useful for handling JSON data.

Reading JSON

python
Copy code
import json

with open("data.json", "r") as file:
    data = json.load(file)
    print(data)

Writing JSON

python
Copy code
with open("output.json", "w") as file:
    json.dump({"name": "Alice", "age": 30}, file, indent=4)

6. Handling Errors

When working with files, errors like missing files or permissions issues can occur. Use try-except to handle them gracefully.

Example:

python
Copy code
try:
    with open("nonexistent.txt", "r") as file:
        content = file.read()
except FileNotFoundError:
    print("File not found!")
except IOError:
    print("An error occurred while accessing the file.")

7. Managing File Paths

Python’s os and pathlib modules help manage file paths across platforms.

Using os

python
Copy code
import os

file_path = os.path.join("folder", "file.txt")
print(file_path)  # Output depends on your OS

Using pathlib

python
Copy code
from pathlib import Path

file_path = Path("folder") / "file.txt"
print(file_path)

8. Practical Example

Problem: Processing a Log File

Read a log file, filter specific entries, and save them to a new file.

Code Example:

python
Copy code
with open("log.txt", "r") as file:
    lines = file.readlines()

filtered = [line for line in lines if "ERROR" in line]

with open("error_logs.txt", "w") as file:
    file.writelines(filtered)

Conclusion

File handling in Python opens doors to working with a wide variety of data sources. Whether it’s reading plain text, parsing CSV files, or handling JSON data, mastering file operations is an indispensable skill.