Beyond the Basics: Working with Files

3.3. Beyond the Basics: Working with Files#

A lot of the work you’ll do in Python will have the following structure:

  1. Read data from a file

  2. Perform computations on the data

  3. Visualize the results and/or save the results to a file

3.3.1. File paths#

File paths on computers work based on a hierarchical structure, often represented as a tree. The root directory serves as the starting point, typically represented by a drive letter (e.g., C: on Windows). From the root directory, you can navigate to other directories using a delimiter character (\ on Windows or / on Unix-based systems). Each directory can contain files and subdirectories, forming a hierarchical structure.

Absolute paths specify the complete path from the root directory, while relative paths are relative to the current working directory. By understanding and manipulating file paths, you can effectively locate and access files and directories on a computer’s file system.

3.3.2. pathlib and os modules#

The os module in Python provides functions for interacting with the operating system, offering operations related to file management, directory handling, process management, and environment variables. It allows you to perform tasks such as creating, deleting, and modifying files and directories, launching external processes, accessing and modifying environment variables, and writing platform-independent code.

On the other hand, the pathlib module introduced in Python 3.4 offers an object-oriented approach to working with file paths and directories. It provides the Path class, which represents paths as objects, allowing for more intuitive and expressive manipulation of paths compared to the traditional string-based operations in os. With pathlib, you can perform operations like joining paths, checking file existence, accessing file attributes, and creating directories in a more convenient and readable manner.

The table below summuraizes some codes you can use for creating and adjusting your own file paths:

Code

Result

Description

os.path.join(path, *paths)

Path string

Joins one or more path components intelligently. It concatenates the arguments using the appropriate path delimiter for the operating system.

os.path.abspath(path)

Absolute path string

Returns the absolute path of the specified path. It resolves any symbolic links and references to parent directories.

os.path.exists(path)

Boolean

Checks if the specified path exists in the file system. Returns True if the path exists, and False otherwise.

os.path.isdir(path)

Boolean

Checks if the specified path is a directory. Returns True if the path is a directory, and False otherwise.

os.path.isfile(path)

Boolean

Checks if the specified path is a regular file. Returns True if the path is a file, and False otherwise.

os.path.splitext(path)

Tuple (base, ext)

Splits the specified path into its base name and extension. Returns a tuple where the first element is the base name and the second element is the extension (including the dot).

os.path.basename(path)

Base name string

Returns the base name (the file or directory name) from the specified path.

os.path.dirname(path)

Directory name string

Returns the directory name from the specified path.

Here are some examples of how to use these codes:

import os

path = os.path.join('folder', 'subfolder', 'file.txt')  # Joining path components intelligently using appropriate delimiter.
absolute_path = os.path.abspath(path)  # Getting the absolute path of the specified path.
exists = os.path.exists(path)  # Checking if the specified path exists.
is_directory = os.path.isdir(path)  # Checking if the specified path is a directory.
is_file = os.path.isfile(path)  # Checking if the specified path is a file.
base_name, extension = os.path.splitext(path)  # Splitting the path into base name and extension.
basename = os.path.basename(path)  # Getting the base name (file or directory name) from the path.
dirname = os.path.dirname(path)  # Getting the directory name from the path.

# Printing the information
print("Path:", path)  # Path string
print("Absolute Path:", absolute_path)  # Absolute path string
print("Exists:", exists)  # Boolean indicating if path exists
print("Is Directory:", is_directory)  # Boolean indicating if path is a directory
print("Is File:", is_file)  # Boolean indicating if path is a file
print("Base Name:", basename)  # Base name of the file or directory
print("Directory Name:", dirname)  # Directory name of the path
print("Extension:", extension)  # File extension with the dot
Path: folder\subfolder\file.txt
Absolute Path: c:\Users\ahmed\Documents\GitHub\learn-python\book\03\In_a_Nutshell\folder\subfolder\file.txt
Exists: False
Is Directory: False
Is File: False
Base Name: file.txt
Directory Name: folder\subfolder
Extension: .txt

This code demonstrates the usage of various os.path functions to perform operations on file paths, such as joining paths, obtaining absolute paths, checking existence, identifying directories or files, splitting paths into base names and extensions, and retrieving the base name and directory name from a path. The corresponding outputs are displayed to provide the relevant information.