The pathlib module in Python (introduced in 3.4) offers an object-oriented way to handle filesystem paths. Pathlib treats paths as objects, making file and directory tasks like creating paths, checking existence and reading/writing clearer and easier to work with.
Path class in pathlib module is the main class used to represent and interact with file and directory paths on the actual file system.
Why do we need Pathlib module?
- Provides a cleaner, object-based approach to handling file system paths.
- Simplifies common tasks like reading, writing and verifying files or directories.
- Automatically handles path differences across operating systems (Windows, Linux, macOS).
- Minimizes errors by offering rich methods for path manipulation instead of relying on string operations.
Importing Path Class
Before we can use any features of pathlib, we need to import Path class from pathlib module:
from pathlib import Path
Common File and Directory Operations
The pathlib module simplifies everyday tasks like listing folders, checking file details and reading or writing files. Let’s explore some examples.
Listing Subdirectories
To view all subdirectories within a given directory, use iterdir() method and filter results using .is_dir(). This helps you easily identify folders without scanning files.
Example:
Python
from pathlib import Path
p = Path('/')
for subdir in p.iterdir():
if subdir.is_dir():
print(subdir)
Output/media
/root
/run
/home
/sbin
/var
/bin
/mnt
/opt
/lib64
/sys
/tmp
/usr
/etc
/boot
/proc
/dev
/srv
/lib
Explanation:
- Path('/'): Creates a Path object pointing to root directory (/ in Unix/Linux or C:/ in Windows).
- iterdir(): Returns all immediate entries (files and directories) in path p.
- is_dir(): Checks if the current entry is a directory (ignores files).
Listing Python Source Files in This Directory Tree
To find all Python files (.py) within a folder and its subfolders, use rglob() method from pathlib. It performs a recursive search, making it easy to scan entire directory trees for specific file types.
Example:
Python
from pathlib import Path
p = Path('/')
files = p.rglob('*.py')
for f in files:
print(f)
Output
\ProgramFiles\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2288.0_x64__qbz5n2kfra8p0\Lib\abc.py
\ProgramFiles\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2288.0_x64__qbz5n2kfra8p0\Lib\aifc.py
\ProgramFiles\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2288.0_x64__qbz5n2kfra8p0\Lib\antigravity.py
\ProgramFiles\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2288.0_x64__qbz5n2kfra8p0\Lib\argparse.py
Explanation:
- rglob('*.py'): Recursively searches for all .py files in p and its subdirectories and returns a generator of matching file paths.
- for loop: Iterates through each Python file found by rglob().
Navigating Inside a Directory Tree
pathlib makes directory navigation simple by overloading / operator. This allows you to join paths cleanly and create new Path objects without using manual string concatenation.
Example:
Python
sp = p / 'example.txt'
print(sp)
Output
\example.txt
Explanation:
- / operator: Overloaded by pathlib to join paths in an object-oriented way.
- p / 'example.txt': Creates a new Path object by joining p (a directory) with 'example.txt'
Querying Path Properties
The pathlib module makes it easy to inspect various properties of a file or directory path, such as whether it's absolute, its name, extension, parent directory, etc all using simple methods.
Example:
Python
# Check if the path is absolute
print("Is absolute:", sp.is_absolute())
# Get the file name
print("File name:", sp.name)
# Get the extension
print("Extension:", sp.suffix)
# Get the parent directory
print("Parent directory:", sp.parent)
Output
Is absolute: False
File name: example.txt
Extension: .txt
Parent directory: \
Reading and Writing to a File
pathlib makes file handling easy by allowing you to open, read and write files directly through Path objects no need for manual path strings or separate functions.
Example:
Python
# Reading from a file
with (path / 'example.txt').open('r') as file:
content = file.read()
print(content)
# Writing to a file
with (path / 'output.txt').open('w') as file:
file.write("Hello, GFG!")
Output
First line of text.
Second line of text.
Third line of text.
Explanation:
- open('r') opens file in read mode and open('w') opens file in write mode (creates or overwrites).
- read() reads file's content and write() writes text to file.
Hierarchy of Path Classes in pathlib Module
Representation of relation between various classes of pathlib moduleTypes of Pathlib module
Path classes in pathlib module are divided into Pure paths and Concrete paths. Let's discuss it clearly with examples.
Pure Paths
Pure path classes are used for path computations without accessing the file system. They offer methods for joining, splitting and normalizing paths and are ideal for cross-platform path handling without performing any file I/O.
PurePaths can be instantiated in following ways using:
1. PurePath class:
It allows you to work with and manipulate path strings. When instantiated, it automatically returns a PurePosixPath or PureWindowsPath based on operating system, making it ideal for cross-platform path handling.
Example:
This code imports PurePath class and creates a path object using PurePath('foo/bar') which represents path as a string. Then print() function displays resulting path object.
Python
from pathlib import PurePath
obj = PurePath('foo/bar') # Instantiate PurePath class
print(obj) # prints instance of PurePath class
2. PurePosixPath class:
This class from pathlib module represents UNIX-style paths (used in Linux and macOS). It is meant for systems that use forward slashes / and should not be used on Windows.
Example:
This code imports PurePosixPath class and creates a UNIX-style path object using PurePosixPath('foo/bar'), which represents given path purely as a string.
Python
from pathlib import PurePosixPath
obj = PurePosixPath('foo/bar') # Instantiate PurePosixPath class
print(obj) # print instance of PurePosixPath class
3. PureWindowsPath class:
This class is used to handle Windows-style file system paths. PureWindowsPath understands Windows path conventions (like backslashes and drive letters), making it useful for creating or manipulating Windows paths even on non-Windows systems.
Example:
This code imports PureWindowsPath class and creates a path object using PureWindowsPath('foo/bar'), print() function displays resulting path object using backslashes (\) as per Windows path conventions.
Python
from pathlib import PureWindowsPath
obj = PureWindowsPath('foo/bar') # Instantiate PureWindowsPath class
print(obj) # print instance of PureWindowsPath class
Concrete Paths
Concrete path classes interact with actual file system and support both path manipulations and file I/O operations. They provide methods for reading, writing, checking file existence, creating directories, etc.
We can instantiate a concrete path in following ways using:
1. Path class:
It represents a path and allows for a variety of methods that interact directly with the filesystem. Depending on system Python is running on, Path will behave like either PosixPath or WindowsPath.
Example:
This code imports Path and creates a concrete path object using Path('/usr/local/bin'), which represents a real path in file system. The print() function displays resulting Path object.
Python
from pathlib import Path
obj = Path('/usr/local/bin') # Instantiate Path class
print(obj)
2. PosixPath class:
This class is used specifically in UNIX-like systems (Linux, macOS). It inherits all methods from Path and PurePosixPath. It provides methods for UNIX-specific filesystem interaction.
Example:
This code imports PosixPath class and creates a concrete path object using PosixPath('/usr/local/bin') which represents a Unix-style file system path.
Python
from pathlib import PosixPath
obj = PosixPath('/usr/local/bin') # Instantiate PosixPath class
print(obj)
3. WindowsPath class:
This class is used on Windows systems. It inherits from Path and PureWindowsPath, adapting path operations to Windows standards. This includes handling paths with drive letters and backslashes.
Example:
This code imports WindowsPath class and creates a concrete path object using WindowsPath('C:/Program Files/') which represents a Windows-style file system path.
Python
from pathlib import WindowsPath
obj = WindowsPath('C:/Program Files/') # Instantiate WindowsPath class
print(obj)
OutputWindowsPath('c:/Program Files/')
Below are few methods provided by Path class:
1. cwd() method:
This method returns a new path object which represents current working directory. For instance, calling Path.cwd() will give us path from where your Python script is executed.
Example:
Python
from pathlib import Path
# Get the current working directory name
dir = Path.cwd()
print(dir)
Output/home/guest/sandbox
2. exists() method:
It checks whether specified path exists on the disk. It returns True if the path exists, otherwise False.
Example:
Python
from pathlib import Path
path = '/home/hrithik/Desktop'
obj = Path(path) # Create Path object
print(obj.exists()) # Check if path exists
3. is_dir() method:
This method is used to determine if path points to a directory. It returns True if the path is a directory, otherwise False.
Example:
Python
from pathlib import Path
path = '/home/hrithik/Desktop'
obj = Path(path) # Instantiate the Path class
print(obj.is_dir()) # Check if path refers to directory or not
Similar Reads
OS Path module in Python OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string obje
2 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python | os.path.join() method The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). ExamplePythonimport
4 min read
Python | os.path.join() method The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). ExamplePythonimport
4 min read
Python | os.path.join() method The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). ExamplePythonimport
4 min read
Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read