Exploring the pathlib Module in Python

Posted by Afsal on 28-Jun-2024

The pathlib module, introduced in Python 3.4, provides an object-oriented approach to handling filesystem paths. It offers an intuitive and convenient way to work with paths, replacing many of the older functions found in the os and os.path modules. In this post, we'll explore the capabilities of pathlib, highlighting its key features and how it simplifies path manipulations in Python.

Key Features of pathlib

Object-Oriented Path Manipulation: Pathlib uses the Path class to represent filesystem paths, making path manipulations more readable and intuitive.

Cross-Platform Compatibility: Pathlib abstracts away the differences between operating systems, providing a consistent interface for working with paths on different platforms.

Convenient Methods: The Path class comes with a rich set of methods for common file and directory operations, such as reading, writing, creating, and deleting.

Getting Started with pathlib

Create a Path object

In [6]: from pathlib import Path
In [7]: p = Path('/home/afsal/Documents/index.html')
In [8]: print(p)
/home/afsal/Documents/index.html
In [9]:

Basic Path Operations

Joining Paths

Use the / operator to join paths, making the code cleaner and more readable

In [9]: base_dir = Path('/home/afsal/Documents/')

In [10]: base_dir

Out[10]: PosixPath('/home/afsal/Documents')

In [11]: file_path = base_dir / 'index.html'

In [12]: file_path

Out[12]: PosixPath('/home/afsal/Documents/index.html')

In [13]:

Getting Path Components

You can access different components of a path easily

In [13]: print(file_path.parent)

/home/afsal/Documents

In [14]: print(file_path.name)

index.html

In [15]: print(file_path.stem)

index

In [16]: print(file_path.suffix)

.html

In [17]:

Checking Existence and Types

Check if a path exists and whether it is a file or directory

In [17]: print(file_path.exists())
True

In [18]: print(file_path.is_file())

True

In [19]: print(file_path.is_dir())

False

In [20]:

Reading and Writing Files

Reading a File

You can read the contents of a file directly from a Path object:

In [20]: file_path.read_text()
Out[20]: '<p>Sample P tag</p>\n'
In [21]:

Writing to a File

Similarly, you can write text to a file

In [22]: new_file_path = base_dir / 'new_file.txt'

In [23]: new_file_path.write_text('Hello, World!')

Out[23]: 13

In [24]:

Directory Operations

Creating Directories

Create a new directory (or nested directories):

In [24]: new_dir = base_dir / 'new_subdirectory'
In [25]: new_dir.mkdir(parents=True, exist_ok=True)
In [26]: new_dir.mkdir(parents=True, exist_ok=True)
In [27]: new_dir.mkdir(parents=True, exist_ok=False)

---------------------------------------------------------------------------

FileExistsError                       Traceback (most recent call last)

Cell In[27], line 1

----> 1 new_dir.mkdir(parents=True, exist_ok=False)

File /usr/lib/python3.10/pathlib.py:1175, in Path.mkdir(self, mode, parents, exist_ok)

   1171 """

   1172 Create a new directory at this given path.

   1173 """

   1174 try:

-> 1175 self._accessor.mkdir(self, mode)

   1176 except FileNotFoundError:

   1177 if not parents or self.parent == self:

FileExistsError: [Errno 17] File exists: '/home/afsal/Documents/new_subdirectory'

In [28]:

Iterating Over Directory Contents

List all files and subdirectories within a directory

In [28]: for item in base_dir.iterdir():
...: print(item)

...:

/home/afsal/Documents/new_subdirectory

/home/afsal/Documents/stone_paper_scissors.py

/home/afsal/Documents/backup

/home/afsal/Documents/index_1.html

/home/afsal/Documents/new_file.txt

/home/afsal/Documents/index.html

Glob Patterns

Use glob patterns to find files matching a specific pattern

In [29]: for txt_file in base_dir.glob('*.txt'):
...: print(txt_file)

...:
/home/afsal/Documents/new_file.txt

In [30]: for html_file in base_dir.glob('*.html'):
...: print(html_file)
...:

/home/afsal/Documents/index_1.html

/home/afsal/Documents/index.html

In [31]:

Remove the file

file_path.unlink()

Conclusion

The pathlib module in Python provides a modern and intuitive way to handle filesystem paths. Its object-oriented approach simplifies many common tasks and makes code more readable and maintainable. By using pathlib, you can write cleaner and more cross-platform compatible code for file and directory operations.