Demystifying Python Paths: A Comprehensive Guide to the path Module for Effortless File and Directory Management
Mastering os.path and pathlib Module in Python Programming
Table of contents
- Introduction
- Path
- Types of Path
- Absolute Path
- Examples of absolute paths
- Relative Path
- Examples of relative paths
- Module
- Path Module in Python
- OS Path Module in Python
- os.path.basename()
- os.path.dirname()
- os.path.exists()
- os.path.join()
- os.path.isfile()
- List of methods in Python os.path module
- Python's PathLib Module
- List directories in a target location
- List files in a directory
- Check if directory or file exist
- Accessing the components of a Path
- Join in Pathlib module
- Path.cwd
- Path.home
- Pure paths
- PurePath(\pathsegments*)
- PurePath methods and properties
- Concrete Paths
- List of methods in Python pathlib module
- Conclusion
Introduction
In this article, we'll learn in-depth about os.path and pathlib module in Python programming and its use case. It is a valuable resource to enhance your Python proficiency.
Path
Path (filepath, file path, pathname) in programming simply refers to a formed string /users/..
that serves as the track or address or link to a location of an executable file or folder in a file system. The Operating system in use determines how the path will be constructed.
Types of Path
There are two major types of Path namely:
Absolute Path
An absolute path is a path that makes reference or link to a file location from the start of the hard drive. It shows the different steps in the path until it gets to the location of the file or folder or target location in the filesystem.
Examples of absolute paths
Windows: C:\Users\Downloads\Python\path.py
Linux/Unix: /home/Courses/Python/path.py
MacOS: /Users/Alemsbaja/Desktop/Series/Python/path.py
Relative Path
Relative path points to the location of the target file or folder in relation to the current working directory. It's more concise, efficient and readable than absolute path.
Examples of relative paths
this pattern of path definition will locate the same file or folder as shown in the absolute path example.
Windows: \Python\path.py
Linux/Unix: /Python/path.py
MacOS: /Python/path.py
Website:/series/python
The dot can be used to step out of the current directory to locate a file in another folder. For example:
Let's assume we are in the path.py file and we want to locate a file named game.py
in another (outer) directory called games
within the Courses directory.
Here's a way to do it in relative path:
../../Games/game.py
NB: The interpretation of the relative path is defined in relation to the current working directory or the defined reference point.
Module
Set of functional codes in a file that can be imported or included in a python file. For example:
import os
#os is a module that is now included in this file for example.
The link to the module which will be covered extensively in this series will be added here.
Path Module in Python
There are different methods in the Path Module in Python programming for handling path. Python offers two modules to handling path os.path module
and the pathlib module
.
NB: The examples of path are in windows format as at the time of writing this article.
OS Path Module in Python
os.path
module." - Python documentation.The following are the methods under the OS path module in Python.
os.path.basename()
The os.path.basename() method in is used to access the file or folder name on the last part of the pathname after the last forward-slash. For example:
import os
print(os.path.basename("c:/Users/Alemsbaja/Desktop/Python/path.py"))
#path.py
os.path.dirname()
The dirname() method is used to access the part of the pathname from the start to the last forward-slash (exempting the basename). For example:
import os
print(os.path.dirname("c:/Users/Alemsbaja/Desktop/Python/path.py"))
#c:/Users/Alemsbaja/Desktop/Python
os.path.exists()
It checks and returns a boolean value (True or False) if the file or folder specified as the argument exist in the filesystem of the operating system. For example:
import os
print(os.path.exists("c:/Users/Alemsbaja/Desktop/Python/path.py"))
# True
os.path.join()
This method takes in string arguments pointing to paths in the filesystem and return a single path. For example:
import os
print(os.path.join('/Users', 'Alemsbaja', 'desktop'))
# /Users\Alemsbaja\desktop
os.path.isfile()
It checks and returns a boolean value (True or False) if the path passed as the argument points to a file or not in the filesystem of the operating system. For example:
import os
print(os.path.isfile("c:/Users/Alemsbaja/Desktop/Python/path.py"))
# True
os.path.normpath()
The normpath method remove extra slashes or change backslash to forwardslash. For example:
import os
print(os.path.normpath("c:/Users/Alemsbaja//Desktop///Python/path.py"))
# c:\Users\Alemsbaja\Desktop\Python\path.py
List of methods in Python os.path module
There are numerous methods in the os.path python module for more functionalities when dealing with paths.
The inspect module can be used to display the list of functions in a module. for example:
import os.path as path
import inspect
functions = inspect.getmembers(path, inspect.isfunction)
for i in functions:
print(i)
- The help method can be used to display full details about the os.path module.
import os
print(help(os.path))
Python's PathLib Module
Worthy of mention for directory and file management is the pathlib
module introduced in Python 3.4. It creates an actual path class for the platform that the code is executing on same way it works on different operating system. Classes that represent filesystem paths with semantics suitable for various operating systems are provided by this module.
from pathlib import Path
print(Path)
#<class 'pathlib.Path'>
Let's look at some examples of Pathlib module methods in Python programming.
List directories in a target location
The Path('.') indicates the directories in the current folder. Double ..
in the Path method steps outside the current working directory. Path('../..')
To move to the outer most directory.
from pathlib import Path
p = Path('..')
print([x for x in p.iterdir() if x.is_dir()])
#[WindowsPath('../current'), WindowsPath('../HitFilm Express'),
List files in a directory
To list files in a directory with a particular extension for example .py
in the pathlib module.
from pathlib import Path
p = Path('.')
print(list(p.glob('**/*.py')))
#[WindowsPath('directory.py'), WindowsPath('path.py')]
Check if directory or file exist
A boolean value of True or False is returned if the file or directory exists using exists() or is_dir() (for directory check) methods from pathlib module in Python.
from pathlib import Path
p = Path('.')
#Querying path properties
print(p.exists())
#True
print(p.is_dir())
#True
Accessing the components of a Path
The following properties can be used to access the several part of a path.
from pathlib import Path, PureWindowsPath, PurePath
path_item = Path('../Python/path.py')
#Parts of the path
print(path_item.parts)
#('..', 'Python', 'path.py')
print(path_item.name)
#Output: path.py
#stem
print(path_item.stem)
#Output: path
#suffix
print(path_item.suffix)
#Output: .py
#anchor
print(path_item.anchor)
#Output:
#parent
print(path_item.parent)
#Output: ..\Python
Join in Pathlib module
Just like os.path.join() the join can be performed by the path module to return a single path from the path defined. For example:
from pathlib import Path
join_path = Path('/Users') / 'Alemsbaja' / 'desktop'
print(join_path)
# \Users\Alemsbaja\desktop
Path.cwd
The path.cwd()
returns the path of the current working directory like getcwd() in the os module.
from pathlib import Path
current_path = Path.cwd()
print(f'The path of the current working directory: {current_path} ')
#The path of the current working directory: C:\....\Desktop\Python
Path.home
Returns the path to the users (owner) directory of the current file or the target destination.
from pathlib import Path
Path.home()
#C:\Users\Alemsbaja
Pure paths
Pure path is a generic class that creates a PurePosixPath
or a PureWindowsPath
object for manipulation upon instantiation that represents the system's path flavour without accessing the operating system.
from pathlib import PurePath
path_item = PurePath('path.py')
print(path_item)
print(type(path_item))
#Output
#path.py
#<class 'pathlib.PureWindowsPath'>
In this article the PurePath
is executed in the windows machine we got PureWindowsPath
but on a Unix machine it'll return PurePosixPath
which are the two subclasses in PurePath()
- The PureWindowsPath class is used for for Windows filesystem paths.
from pathlib import PureWindowsPath
path_item = PureWindowsPath('path.py')
print(path_item)
print(type(path_item))
#Output
#path.py
#<class 'pathlib.PureWindowsPath'>
- The PurePosixPath represents non-Windows file system paths.
from pathlib import PurePosixPath
path_item = PurePosixPath('path.py')
print(path_item)
print(type(path_item))
#Output
#path.py
#<class 'pathlib.PurePosixPath'>
PurePath(\pathsegments*)
The *pathsegments passed to the PurePath method returns a single path.
Joining path using
from pathlib import PurePath
print(PurePath('Users', 'Alemsbaja/Desktop/Python', 'path.py'))
#Users\Alemsbaja\Desktop\Python\path.py
PurePath methods and properties
The methods and properties in PurePath in action:
from pathlib import PurePath
path_item = PurePath('path.py')
#type
print(type(path_item))
#output
#<class 'pathlib.PureWindowsPath'>
#name
print(path_item.name)
#path.py
#drive
print(f"With Drive: {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').drive}")
#Output
#With Drive: c:
#withoutdrive
print(f"Without Drive: {PurePath('Desktop/Python/path.py').drive}")
#Output
# Without Drive:
#root
print(f"Root: {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').root}")
#Output
# Root: \
#stem
print(f"Stem: {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').stem}")
#Output
# Stem: path
print(f"Accesing the parent {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').parent}")
#Output
# Accesing the parent c:\Users\Alemsbaja\Desktop\Python
# Accesing the ancestors
print(f"Accesing the first ancestor {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').parents[0]}")
#Output
# Accesing the first ancestor c:\Users\Alemsbaja\Desktop\Python
print(f"Accesing the second ancestor {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').parents[1]}")
#Output
# Accesing the second ancestor c:\Users\Alemsbaja\Desktop
print(f"Accesing the third ancestor {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').parents[2]}")
#Output
# Accesing the third ancestor c:\Users\Alemsbaja
print(f"Accesing the fourth ancestor {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').parents[3]}")
#Output
# Accesing the fourth ancestor c:\Users
print(f"Accesing the fifth ancestor {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').parents[4]}")
#Output
# Accesing the fifth ancestor c:\
print(f"Check if absolute: {PurePath('c:/Users/Alemsbaja/Desktop/Python/path.py').is_absolute()}")
#Output
# Check if absolute: c:\Users\Alemsbaja\Desktop\Python\path.py
#Join Path
print(f"Check if absolute: {PurePath('c:/Users/Alemsbaja/Desktop/Python').joinpath('path.py')}")
#Output
#Check if absolute: c:\Users\Alemsbaja\Desktop\Python\path.py
Concrete Paths
Input/output and computational operations are carried out by concrete paths. It can do tasks like reading and writing data to and from files, as well as interacting with them. Concrete paths are subclasses of the pure path classes.
Three ways to instantiate concrete paths
Path()
WindowsPath()
PosixPath()
The concrete path can perform the following operations in addition to the functions in pure path:
Path.cwd()
Path.home()
Path.chmod()
Path.exists()
Path.glob() etc...
List of methods in Python pathlib module
This article does not cover all the methods available in the pathlib Python module. Here's a way to view the available methods in the pathlib Python module.
from pathlib import Path
import inspect
functions = inspect.getmembers(Path, inspect.isfunction)
print(len(functions))
for i in functions:
print(i)
There are about 65 functions as at the time of writing this article.
The functions mentioned in these articles titled: A Complete Guide to Python File Handling: Data Manipulation Made Simple and A Comprehensive Guide for Understanding Directory in Python Programming can be performed using the pathlib module methods in python.
NB:Correspondence to tools in the os module to path module
For more information on Python programming pathlib module check out the documentation.
Conclusion
The os.path
or pathlib
module in Python can handle file and directory paths when programming. The pathlib was added in Python 3.4 with concise and easy to use file management methods. Whether you're a novice or an experienced Python developer, this article delves into the significance of paths, illustrating practical scenarios where the path
module proves invaluable. The functions and methods for creating, manipulating, and resolving paths, emphasizing the module's role in enhancing cross-platform compatibility are covered with PurePath and ConcretePath classes. The article serves as a comprehensive guide, empowering readers to navigate the intricacies of path and file management in Python programming.
Find this helpful or resourceful? kindly share with others and feel free to use the comment section for questions, answers, and contributions.