Image by the author
In Python, using regular strings for file system paths can be a problem, especially if you need to perform operations on the path strings. Switching to a different operating system also causes breaking changes to your code. Yes, you can use operating system path from the operating system module to make things easier. But the route library The module makes all this much more intuitive.
The pathlib module introduced in Python 3.4 (yes, it's been around for a while now) enables an OOP approach to creating and working with path objects, and comes with built-in stacks for common operations like joining and manipulating paths, resolving paths, and more.
This tutorial will teach you how to work with the file system using the pathlib module. Let's get started.
Working with route objects
To start using pathlib, you must first import the Path
class:
Which allows you to instantiate path objects to create and manipulate file system paths.
Creating route objects
You can create a Path
object passing a string representing the path as follows:
path = Path('your/path/here')
You can also create new path objects from existing paths. For example, you can create path objects from your home directory or current working directory:
home_dir = Path.home()
print(home_dir)
cwd = Path.cwd()
print(cwd)
This should give you a similar result:
Output >>>
/home/balapriya
/home/balapriya/project1
Suppose you have a base directory and you want to create a path to a file inside a subdirectory. You can do this as follows:
from pathlib import Path
# import Path from pathlib
from pathlib import Path
# create a base path
base_path = Path("/home/balapriya/Documents")
# create new paths from the base path
subdirectory_path = base_path / "projects"https://www.kdnuggets.com/"project1"
file_path = subdirectory_path / "report.txt"
# Print out the paths
print("Base path:", base_path)
print("Subdirectory path:", subdirectory_path)
print("File path:", file_path)
This first creates a path object for the base directory: /home/balapriya/Documents
. Remember to replace this base path with a valid file system path in your working environment.
Then create subdirectory_path
joining together base_path
with subdirectories projects
and project1
. Finally, the file_path
It is created by joining subdirectory_path
with the name of the file report.txt
.
As you can see, you can use the /
operator to add a directory or file name to the current path, creating a new path object. Note how the overload of the /
The operator provides a readable and intuitive way to join routes.
When you run the above code, it will generate the following routes:
Output >>>
Base path: /home/balapriya/documents
Subdirectory path: /home/balapriya/documents/projects/project1
File path: /home/balapriya/documents/projects/project1/report.txt
Status check and route types
Once you have a valid route object, you can call simple methods to check the state and type of the route.
To check if a route exists, call exists()
method:
path = Path("/home/balapriya/Documents")
print(path.exists())
If the route exists, it is displayed True
; otherwise, return False
.
You can also check if a path is a file or directory:
print(path.is_file())
print(path.is_dir())
Note:An object of the
Path
The class creates a concrete path for your operating system. But you can also usePurePath
when you need to handle paths without accessing the file system, such as working with Windows paths on a Unix machine.
Navigating the file system
Navigating the file system is pretty straightforward with pathlib. You can iterate over the contents of directories, rename and resolve paths, and much more.
You can call iterdir()
method on the path object as follows to iterate over the entire contents of a directory:
path = Path("/home/balapriya/project1")
# iterating over directory contents
for item in path.iterdir():
print(item)
Here is the example output:
Output >>>
/home/balapriya/project1/test.py
/home/balapriya/project1/main.py
Rename files
You can rename files by calling rename()
method on the route object:
path = Path('old_path')
path.rename('new_path')
Here, we changed the name test.py
in it project1
directory to tests.py
:
path = Path('/home/balapriya/project1/test.py')
path.rename('/home/balapriya/project1/tests.py')
You can now cd
in it project1
directory to check if the file has been renamed.
Delete files and directories
You can also delete a file and remove empty directories with the unlink()
for and rmdir()
methods, respectively.
# For files
path.unlink()
# For empty directories
path.rmdir()
Note: Well, in case deleting empty directories has made you curious about creating them. Yes, you can also create directories with
mkdir()
like:path.mkdir(parents=True, exist_ok=True)
. Hemkdir()
The method creates a new directory. Configurationparents=True
allows the creation of home directories as needed, andexist_ok=True
avoid errors if the directory already exists.
Absolute path resolution
Sometimes it is easier to work with relative paths and expand them to the absolute path when needed. You can do this with the resolve()
method, and the syntax is super simple:
absolute_path = relative_path.resolve()
Here is an example:
relative_path = Path('new_project/README.md')
absolute_path = relative_path.resolve()
print(absolute_path)
And the result:
Output >>> /home/balapriya/new_project/README.md
File Globbing
Global encoding is very useful for finding files that match specific patterns. Let's take an example directory:
projectA/
├── projectA1/
│ └── data.csv
└── projectA2/
├── script1.py
├── script2.py
├── file1.txt
└── file2.txt
Here is the way:
path = Path('/home/balapriya/projectA')
Let's try to find all text files using glob()
:
text_files = list(path.glob('*.txt'))
print(text_files)
Surprisingly, we don't get the text files. The list is empty:
This is because these text files are in the subdirectory and glob does not search in subdirectories. Enter recursive globbing with rglob()
.
text_files = list(path.rglob('*.txt'))
print(text_files)
He rglob()
The method performs a recursive search for all text files in the directory and all its subdirectories. Therefore, we should get the expected result:
Output >>>
(PosixPath('/home/balapriya/projectA/projectA2/file2.txt'),
PosixPath('/home/balapriya/projectA/projectA2/file1.txt'))
And that's a wrap!
Ending
In this tutorial, we've explored the pathlib module and how it makes file system navigation and manipulation in Python accessible. We've covered enough ground to help you create and work with file system paths in Python scripts.
You can find the code used in this tutorial. on GitHubIn the next tutorial we will see interesting practical applications. Until then, keep coding!
twitter.com/balawc27″ rel=”noopener”>girl priya c Bala is a technical developer and writer from India. She enjoys working at the intersection of mathematics, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, programming, and drinking coffee! Currently, she is working on learning and sharing her knowledge with the developer community by creating tutorials, how-to guides, opinion pieces, and more. Bala also creates interesting resource overviews and coding tutorials.
<script async src="//platform.twitter.com/widgets.js” charset=”utf-8″>