What is Pathlib
Pathlib is a module in Python's standard library that simplifies the way you interact with the file system. It provides an object-oriented interface for representing file paths and provides methods for performing common file system operations such as reading, writing, and manipulating files and directories.
Prior to Python 3.4, developers had to rely on the os and os.path modules to perform file system operations. However, these modules were somewhat cumbersome to use and lacked the object-oriented interface provided by pathlib.
Pathlib simplifies file system operations by providing a high-level interface that can be used to manipulate paths and files. With pathlib, you can easily create, read, write, and manipulate files and directories without worrying about platform-specific path separators or worrying about concatenating paths using string manipulation.
Basic Usage of pathlib
This article wll cover the basic usage of pathlib and how it simplifies working with file paths.
Creating Paths
To create a Path
object, you simply pass a string representing the path to the Path
constructor. For example:
from pathlib import Path
# Create a path object
path = Path('/usr/local/bin')
You can also create a Path
object using a relative path.
# Create a path object using a relative path
path = Path('foo/bar')
Working with Paths
Pathlib provides a number of methods for working with paths. One of the most common is the joinpath()
method, which allows you to join two paths together. For example:
# Join two paths
path = Path('/usr/local').joinpath('bin')
You can also access individual parts of a path using attributes like parent
, name
, and suffix
. For example:
# Access parts of a path
path = Path('/usr/local/bin/python3')
print(path.parent) # /usr/local/bin
print(path.name) # python3
print(path.suffix) # .py
Pathlib also provides methods for checking if a path exists, getting the file size, and deleting a file.
# Check if a path exists
print(path.exists())
# Get the size of a file
print(path.stat().st_size)
# Delete a file
path.unlink()
Methods of Path Object
At the core of pathlib is the Path
class, which represents a file or directory path in a platform-independent way. Path
objects provide a number of methods that can be used to work with file and directory paths. This article will explore some of the most commonly used methods of the Path
object in pathlib and provide examples of how to use them.
exists()
exists()
returns True
if the file or directory exists, False
otherwise.
from pathlib import Path
# Create a Path object
file_path = Path('/path/to/my/file')
# Check if the file exists
if file_path.exists():
print('The file exists!')
is_file()
is_file()
returns True
if the path represents a file, False
otherwise.
from pathlib import Path
# Create a Path object
file_path = Path('/path/to/my/file')
# Check if the path is a file
if file_path.is_file():
print('The path is a file!')
is_dir()
is_dir()
returns True
if the path represents a directory, False
otherwise.
from pathlib import Path
# Create a Path object
dir_path = Path('/path/to/my/directory')
# Check if the path is a directory
if dir_path.is_dir():
print('The path is a directory!')
name
name
returns the name of the file or directory without the path.
from pathlib import Path
# Create a Path object
file_path = Path('/path/to/my/file')
# Get the name of the file
file_name = file_path.name
print('File name:', file_name)
parent
parent
returns the parent directory of the file or directory.
from pathlib import Path
# Create a Path object
file_path = Path('/path/to/my/file')
# Get the parent directory of the file
parent_dir = file_path.parent
print('Parent directory:', parent_dir)
absolute()
absolute()
returns the absolute path of the file or directory.
from pathlib import Path
# Create a Path object
file_path = Path('/path/to/my/file')
# Get the absolute path of the file
abs_path = file_path.absolute()
print('Absolute path:', abs_path)
glob(pattern)
glob(pattern)
Returns a list of Path objects that match the specified pattern.
from pathlib import Path
# Create a Path object for a directory
dir_path = Path('/path/to/my/directory')
# Get a list of all the PDF files in the directory
pdf_files = dir_path.glob('*.pdf')
for pdf_file in pdf_files:
print(pdf_file)
cwd()
cwd()
returns a Path object representing the current working directory.
from pathlib import Path
current_directory = Path.cwd()
print(f"Current directory: {current_directory}")
Absolute Path and Relative Path
Pathlib provides a way to convert between absolute paths and relative paths using the resolve()
and relative_to()
methods of the Path object.
The resolve()
method is used to convert a relative path to an absolute path. It takes no arguments and returns a new Path object representing the absolute path. Here's an example:
from pathlib import Path
relative_path = Path('my_folder', 'my_file.txt')
absolute_path = relative_path.resolve()
print(f'Relative path: {relative_path}')
print(f'Absolute path: {absolute_path}')
In this example, we create a new Path object representing a relative path my_folder/my_file.txt
. We then use the resolve()
method to convert it to an absolute path. The output will be:
Relative path: my_folder/my_file.txt
Absolute path: /full/path/to/my_folder/my_file.txt
The relative_to()
method is used to convert an absolute path to a relative path. It takes one argument, which is the base directory that the resulting relative path should be relative to. Here's an example:
from pathlib import Path
base_path = Path('/full/path/to/my_folder')
absolute_path = Path('/full/path/to/my_folder/my_file.txt')
relative_path = absolute_path.relative_to(base_path)
print(f'Absolute path: {absolute_path}')
print(f'Relative path: {relative_path}')
In this example, we create two Path objects representing an absolute path '/full/path/to/my_folder/my_file.txt'
and a base directory '/full/path/to/my_folder'
. We then use the relative_to()
method to convert the absolute path to a relative path relative to the base directory. The output will be:
Absolute path: /full/path/to/my_folder/my_file.txt
Relative path: my_file.txt
Using these methods, you can easily convert between absolute and relative paths in your code, making it easier to work with files and directories regardless of their location on the file system.
Joining Paths
Joining paths is a common operation when working with file and directory paths in Python. The Path
object in the pathlib module provides a simple way to join paths using the joinpath()
method.
The joinpath()
method takes one or more arguments, which can be strings or other Path
objects. It concatenates these paths together to form a new Path
object. Here's an example:
from pathlib import Path
path1 = Path('/Users/myuser')
path2 = Path('Documents', 'file.txt')
full_path = path1.joinpath(path2)
print(full_path)
In this example, we create two Path objects representing the paths /Users/myuser
and Documents/file.txt
. We then use the joinpath()
method to concatenate these paths together into a new Path object representing the full path /Users/myuser/Documents/file.txt
.
You can also pass multiple arguments to joinpath()
at once, like this:
from pathlib import Path
path1 = Path('/Users/myuser')
path2 = 'Documents'
path3 = 'file.txt'
full_path = path1.joinpath(path2, path3)
print(full_path)
This will produce the same output as the previous example.
You can also use the /
operator to join paths together. This is equivalent to calling joinpath()
. Here's an example:
from pathlib import Path
path1 = Path('/Users/myuser')
path2 = Path('Documents')
path3 = Path('file.txt')
full_path = path1 / path2 / path3
print(full_path)
This will produce the same output as the previous examples.
Handling Exceptions with pathlib
In any program that interacts with files and directories, there is always the potential for errors to occur. For example, a file might not exist, a directory might not be writable, or the program might not have the necessary permissions to access a file. In Python, you can use exceptions to handle these errors and gracefully handle them. In this article, I'll cover how to handle exceptions with pathlib.
Exceptions in Pathlib
Pathlib raises exceptions when errors occur. These exceptions are derived from the base class OSError
and provide additional information about the error. Some common exceptions raised by pathlib include:
FileNotFoundError
: raised when a file or directory does not exist.PermissionError
: raised when the program does not have sufficient permissions to access a file or directory.NotADirectoryError
: raised when a path that is expected to be a directory is not actually a directory.
Handling Exceptions with Try-Except Blocks
To handle exceptions raised by pathlib, you can use a try-except block. This allows you to gracefully handle errors and prevent your program from crashing. Here's an example:
from pathlib import Path
# Create a path object for a file
file_path = Path('/path/to/my/file')
try:
# Open the file for reading
with file_path.open() as f:
contents = f.read()
except FileNotFoundError:
# Handle the error if the file doesn't exist
print('Error: File does not exist.')
except PermissionError:
# Handle the error if we don't have permission to access the file
print('Error: Permission denied.')
In this example, we're trying to open a file for reading using the open()
method. If the file does not exist, we'll catch the FileNotFoundError exception and handle it by printing an error message. Similarly, if we don't have permission to access the file, we'll catch the PermissionError
exception and handle it by printing another error message.
Example Path Manager Class
Here's a sample code for a path manager class that returns absolute paths and relative paths:
from pathlib import Path
class PathManager:
def __init__(self):
self.root_path = Path(__file__).resolve().parents[2]
def get_absolute_path(self, path_str):
path = Path(path_str)
return path.resolve()
def get_relative_path(self, path_str):
path = Path(path_str)
return path.relative_to(self.root_path)
The __init__
method sets the root_path
attribute to the third parent directory of the current file, which can be used as the base path for relative paths.
The get_absolute_path
method takes a path string and returns the absolute path using the resolve()
method of the Path
class.
The get_relative_path
method takes a path string and returns the relative path from the root_path
using the relative_to()
method of the Path
class.
You can use the class like this:
pm = PathManager()
abs_path = pm.get_absolute_path("data/file.txt")
rel_path = pm.get_relative_path("src/utils.py")
References