left-icon

Essential Python Succinctly®
by Ed Freitas

Previous
Chapter

of
A
A
A

CHAPTER 5

File Handling


Quick intro

File handling in Python is a fundamental skill for many applications, allowing you to read, write, and manipulate files. This section will cover the basics of reading and writing different file formats, like text, CSV, and JSON, and more advanced file operations using the OS and shutil modules.

Note: You can run the code following examples by opening the built-in terminal within VS Code and executing the command: python <script>.py. Replace <script> with the name of the respective Python file to execute.

Reading and writing text files

Python provides built-in functions to open, read, and write files, especially with text-based data. Let’s start by understanding how to handle essential text files, and then we’ll dive into CSV and JSON files.

To open a text file, you can use the open() function, which accepts the file path and a mode ('r' for read, 'w' for write, 'a' for append).

Code Listing 5-a: text.py

# Writing to a text file

with open("example.txt", "w") as file:

    file.write("Hello, this is a sample text file.\n")

    file.write("File handling is essential in Python.\n")

# Reading from a text file

with open("example.txt", "r") as file:

    content = file.read()  # Read the entire file content

    print(content)

We open example.txt in "w" (write) mode. The with statement ensures that the file is correctly closed after completing operations. Then file.write() writes strings to the file. Each write call adds text as a new line in the file.

The file is opened in "r" (read) mode, and file.read() reads the entire file content. This content is then printed to the console.

Furthermore, the Python with statement will automatically close open files when the block finishes execution. Although it is possible to open and close files explicitly, this technique is not recommended.

Reading and writing CSV files

The CSV module in Python makes it easy to work with CSV files, which are commonly used for data storage and exchange.

Code Listing 5-b: csvfiles.py

import csv

# Writing to a CSV file

with open("data.csv", "w", newline="") as file:

    writer = csv.writer(file)

    writer.writerow(["Name", "Age", "Country"])

    writer.writerow(["Alice", 30, "USA"])

    writer.writerow(["Bob", 25, "UK"])

# Reading from a CSV file

with open("data.csv", "r") as file:

    reader = csv.reader(file)

    for row in reader:

        print(row)

Explanation:

·     Writing to a CSV file:

o     We use csv.writer() to create a writer object to write rows to the CSV file.

o     writer.writerow() writes each row to the CSV, where each element in the list becomes a cell in the row.

·     Reading from a CSV file:

o     csv.reader() creates a reader object, allowing us to loop through each row.

o     Each row read is a list of strings, making processing data line by line easy.

Reading and writing JSON files

The json module is invaluable for working with JSON, a format widely used in web APIs and data storage. Let’s look at the following code.

Code Listing 5-c: jsonfiles.py

import json

# Writing to a JSON file

data = {

    "name": "Alice",

    "age": 30,

    "is_employee": True,

    "skills": ["Python", "Data Analysis", "Machine Learning"]

}

with open("data.json", "w") as file:

    json.dump(data, file, indent=4)

# Reading from a JSON file

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

    data_loaded = json.load(file)

    print(data_loaded)

Explanation:

·     Writing JSON data:

o     json.dump() writes a Python dictionary (or list) to a JSON file.

o     The indent=4 argument makes the JSON readable by formatting it with four spaces per indentation level.

·     Reading JSON data:

o     json.load() reads JSON data from a file and converts it to a Python dictionary or list, making it easy to work with structured data.

File ops

The os and shutil modules provide powerful tools for file and directory manipulation. You can create, move, copy, and delete files and directories, making managing your file system programmatically more accessible.

Let’s look at an example demonstrating basic operations such as creating directories, moving files, and deleting them.

Code Listing 5-d: fileops.py

import os

import shutil

# Create a new directory

os.makedirs("test_dir/sub_dir", exist_ok=True)

# Create a new file in the directory

with open("test_dir/sample.txt", "w") as file:

    file.write("This is a sample file.")

# Move the file to a new location

shutil.move("test_dir/sample.txt", "test_dir/sub_dir/sample.txt")

# Copy the file to a new location

shutil.copy("test_dir/sub_dir/sample.txt", "test_dir/sample_copy.txt")

# Delete the file and directory

os.remove("test_dir/sample_copy.txt")

shutil.rmtree("test_dir")  # Deletes test_dir and all its contents

Explanation:

·     Creating directories:

o     os.makedirs() creates directories, including any necessary parent directories. The exist_ok=True argument ensures that no error occurs if the directory already exists.

·     Moving files:

o     shutil.move() moves a file from one location to another.

·     Copying files:

o     shutil.copy() creates a copy of the file at the specified location.

·     Deleting files and directories:

o     os.remove() deletes a single file.

o     shutil.rmtree() removes an entire directory tree, deleting the specified directory and all its contents.

Recap

You can manage and process data stored in various file formats by mastering file handling in Python.

Whether working with text, CSV, or JSON files or performing file operations with the os and shutil modules, these techniques are invaluable for developing applications that interact with the file system.

Scroll To Top
Disclaimer

DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.