Skip to content

11 ๐ŸŒ File Handling

File handling is an essential part of programming, allowing reading, writing, and manipulating files. In data science, machine learning, and AI, working with text files, CSV files, and logs is common for storing and processing large datasets.

This chapter covers file handling operations, including reading, writing, appending, working with different file formats, and error handling.


11.1 ๐Ÿ“‚ Working with Files in Python

Python provides built-in functions for opening, reading, writing, and closing files.

โœ… Opening a File

file = open("example.txt", "r")  # Open a file in read mode
print(file.read())  # Read the entire file content
file.close()  # Always close the file after use

โœ… Use Case: Reading log files, dataset files, configuration files.


11.2 ๐Ÿ“ Writing to a File

To write to a file, use write ('w') mode. If the file doesnโ€™t exist, Python creates a new file.

โœ… Writing Data

file = open("example.txt", "w")  # Open in write mode
file.write("Hello, Python!\nWelcome to File Handling.")
file.close()

๐Ÿ”น Note: 'w' mode overwrites existing content. To append, use 'a'.

โœ… Appending Data to an Existing File

file = open("example.txt", "a")
file.write("\nThis is an additional line.")
file.close()

โœ… Use Case: Logging data, saving AI/ML model outputs.


11.3 ๐Ÿ“– Reading Files Efficiently

Python provides different ways to read files efficiently.

โœ… Reading a File Line by Line

file = open("example.txt", "r")

for line in file:
    print(line.strip())  # Removing extra newlines
file.close()

โœ… Use Case: Processing large datasets without memory overflow.

๐Ÿ”น Using readlines() to Read All Lines as a List

file = open("example.txt", "r")
lines = file.readlines()
print(lines)  # Output: List of lines in the file
file.close()

โœ… Use Case: Reading structured text data (e.g., logs, reports).


11.4 ๐Ÿš€ Using with Statement for File Handling

Using with open() ensures that the file automatically closes after execution.

โœ… Safe File Handling with with

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

โœ… Use Case: Better memory management, avoiding file lock issues.


11.5 ๐Ÿ“Š Handling Different File Formats

๐Ÿ”น Working with CSV Files (csv Module)

import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)  # Output: List of values in each row

โœ… Use Case: Reading structured datasets in ML models.

๐Ÿ”น Writing to a CSV File

with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Age"])
    writer.writerow(["Alice", 25])

โœ… Use Case: Saving processed data from AI pipelines.


๐Ÿ”น Working with JSON Files (json Module)

import json

data = {"name": "Alice", "age": 25, "city": "New York"}

# Writing JSON data
with open("data.json", "w") as file:
    json.dump(data, file)

# Reading JSON data
with open("data.json", "r") as file:
    loaded_data = json.load(file)
print(loaded_data)  # Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}

โœ… Use Case: Handling API responses, configuration files, AI model metadata.


11.6 ๐Ÿ› ๏ธ Error Handling in File Operations

Itโ€™s important to handle file-related errors to prevent crashes.

โœ… Handling File Not Found Error

try:
    with open("non_existent.txt", "r") as file:
        content = file.read()
except FileNotFoundError:
    print("Error: File not found!")

โœ… Use Case: Ensuring robust scripts in production AI pipelines.


11.7 ๐Ÿ“‚ Working with Directories (os Module)

The os module allows working with directories and file management.

โœ… Listing Files in a Directory

import os

print(os.listdir("."))  # Lists all files in the current directory

๐Ÿ”น Creating and Deleting Folders

os.mkdir("new_folder")  # Create a folder
os.rmdir("new_folder")  # Remove a folder

โœ… Use Case: Managing dataset directories in ML projects.


Summary

Concept Description Use Case
Reading Files open("file.txt", "r") Loading datasets, logs
Writing Files open("file.txt", "w") Saving AI model results
Appending to Files open("file.txt", "a") Logging incremental data
Using with Statement Ensures safe file handling Avoids resource leaks
CSV Handling csv.reader(), csv.writer() Working with structured data
JSON Handling json.load(), json.dump() API responses, metadata storage
Error Handling try-except for missing files Prevents crashes in AI pipelines
Directory Management os.listdir(), os.mkdir() Managing datasets and logs

Final Thoughts

Mastering file handling and I/O is essential for data processing, logging, and storage in AI, ML, and automation projects.

Would you like real-world coding exercises for practice? ๐Ÿš€