File Handling in Python

File handling is an essential skill for any Python programmer. It allows a program to read data from external sources and write output to files. This is crucial for building real-world applications such as log systems, data processing tools, file-based databases, and configuration managers.

In this chapter, we’ll explore Python’s file I/O capabilities in detail — including how to read from and write to files, handle different file modes, use context managers, deal with exceptions, work with CSV and JSON formats, and follow best practices.

1. Introduction to File Handling

Files are used to store data permanently. Python provides built-in functions to interact with files on disk, enabling persistent storage of data.

Common Use Cases:

  • Logging application output
  • Reading configurations
  • Saving user input
  • Importing datasets for processing
  • Exporting reports

2. Opening a File

Use the built-in open() function.

Syntax:

file = open("filename", "mode")
Code language: JavaScript (javascript)

File Modes:

ModeDescription
'r'Read (default), file must exist
'w'Write, creates or overwrites file
'a'Append, creates file if not exists
'x'Create, fails if file exists
'b'Binary mode
't'Text mode (default)
'r+'Read and write

3. Reading from a File

Example:

file = open("data.txt", "r")
content = file.read()
print(content)
file.close()
Code language: PHP (php)

Other Reading Methods:

file.readline()     # Reads one line
file.readlines()    # Returns list of all lines
Code language: CSS (css)

Reading with Loop:

with open("data.txt") as file:
    for line in file:
        print(line.strip())
Code language: JavaScript (javascript)

4. Writing to a File

Overwrite Mode:

file = open("output.txt", "w")
file.write("Hello, world!\n")
file.close()
Code language: JavaScript (javascript)

Append Mode:

with open("output.txt", "a") as file:
    file.write("New line\n")
Code language: JavaScript (javascript)

5. Using with Statement (Context Manager)

Automatically handles closing files.

with open("data.txt", "r") as file:
    content = file.read()
Code language: JavaScript (javascript)

Advantages:

  • No need to call close()
  • Cleaner code
  • Prevents memory leaks

6. Working with Binary Files

Used for images, audio, or serialized objects.

with open("image.jpg", "rb") as file:
    data = file.read()
Code language: JavaScript (javascript)

To write binary data:

with open("copy.jpg", "wb") as file:
    file.write(data)
Code language: JavaScript (javascript)

7. Handling File Exceptions

Example:

try:
    with open("file.txt", "r") as f:
        data = f.read()
except FileNotFoundError:
    print("File not found.")
except IOError:
    print("Error reading file.")
Code language: PHP (php)

Common Exceptions:

  • FileNotFoundError
  • PermissionError
  • IsADirectoryError
  • IOError

8. Working with File Paths

Use the os and pathlib modules.

import os
path = os.path.join("folder", "file.txt")

from pathlib import Path
path = Path("folder") / "file.txt"
Code language: JavaScript (javascript)

Check existence:

os.path.exists("file.txt")
Code language: CSS (css)

9. File Object Methods

MethodDescription
.read()Read entire file
.readline()Read single line
.readlines()Read all lines as list
.write()Write string to file
.writelines()Write list of strings to file
.seek()Move file pointer to specific byte
.tell()Returns current file pointer location
.close()Closes file

10. File Pointer and seek()

Example:

file = open("data.txt", "r")
print(file.tell())     # Position = 0
file.read(5)
print(file.tell())     # Position = 5
file.seek(0)           # Go back to start
file.close()
Code language: PHP (php)

11. Working with CSV Files

Python’s csv module simplifies working with comma-separated values.

import csv

with open("data.csv", "r") as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
Code language: JavaScript (javascript)

Writing to CSV:

with open("output.csv", "w", newline='') as f:
    writer = csv.writer(f)
    writer.writerow(["Name", "Age"])
    writer.writerow(["Alice", 25])
Code language: JavaScript (javascript)

Using DictReader and DictWriter:

with open("data.csv") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row["Name"])
Code language: JavaScript (javascript)

12. Working with JSON Files

JSON is commonly used for config files, web APIs.

import json

# Reading JSON
with open("data.json", "r") as f:
    data = json.load(f)

# Writing JSON
with open("output.json", "w") as f:
    json.dump(data, f, indent=4)
Code language: PHP (php)

13. File Handling in Different Encodings

Specify encoding when working with non-ASCII text.

with open("data.txt", "r", encoding="utf-8") as f:
    content = f.read()
Code language: JavaScript (javascript)

Other encodings:

  • utf-16
  • latin-1
  • ascii

14. Real-World Applications

  • Log management: Save logs for debugging
  • Web scraping: Save extracted data to files
  • Configuration: Read settings from JSON/YAML
  • Data transformation: Process CSV files for analytics
  • Data pipelines: Intermediate file-based storage

15. Best Practices

  • Always use with for opening files
  • Handle exceptions using try-except
  • Validate file paths before opening
  • Choose appropriate modes (w, a, x)
  • Use newline and encoding arguments for cross-platform compatibility

16. Exercises

  1. Write a script that reads a file and prints each line in uppercase.
  2. Write a program to copy the contents of one file to another.
  3. Parse a CSV file and print each row’s content.
  4. Create and write a dictionary to a JSON file.
  5. Create a log.txt file and append a new timestamped entry every time the script runs.
  6. Write a program that accepts a filename from the user and checks if it exists.

17. Summary

File handling in Python empowers developers to interact with the filesystem, manage data persistently, and build practical applications. From basic file I/O to structured formats like CSV and JSON, mastering file operations is a key skill for any Python programmer.

Next Chapter: Exception Handling – Learn how to gracefully manage errors and prevent crashes in Python applications.

Leave a Reply

Your email address will not be published. Required fields are marked *