Verify with pathlib.Path
In this step, we'll explore the pathlib
module, which provides an object-oriented way to interact with files and directories. We'll specifically focus on using pathlib.Path
to verify the existence of files and directories, offering an alternative to os.path.exists()
.
The pathlib
module offers a more modern and intuitive approach to file path manipulation compared to the older os.path
module. It represents file paths as objects, allowing you to perform various operations using methods of those objects.
Let's modify the file_paths.py
script we've been working with to use pathlib.Path
. Open the file_paths.py
file in your VS Code editor and add the following code:
import os
from pathlib import Path
## Get the current working directory
current_directory = os.getcwd()
## Define a relative path to the file
relative_path = "my_file.txt"
## Define an absolute path to the file
absolute_path = os.path.join(current_directory, relative_path)
## Create Path objects for relative and absolute paths
relative_path_obj = Path(relative_path)
absolute_path_obj = Path(absolute_path)
## Check if the file exists using the relative path object
if relative_path_obj.exists():
print("The relative path 'my_file.txt' exists (using pathlib).")
else:
print("The relative path 'my_file.txt' does not exist (using pathlib).")
## Check if the file exists using the absolute path object
if absolute_path_obj.exists():
print("The absolute path", absolute_path, "exists (using pathlib).")
else:
print("The absolute path", absolute_path, "does not exist (using pathlib).")
## Check if a directory exists using pathlib
directory_path_obj = Path(current_directory)
if directory_path_obj.exists():
print("The directory", current_directory, "exists (using pathlib).")
else:
print("The directory", current_directory, "does not exist (using pathlib).")
## Check if a non-existent file exists using pathlib
non_existent_file = "non_existent_file.txt"
non_existent_path_obj = Path(non_existent_file)
if non_existent_path_obj.exists():
print("The file", non_existent_file, "exists (using pathlib).")
else:
print("The file", non_existent_file, "does not exist (using pathlib).")
In this script:
- We import the
Path
class from the pathlib
module.
- We create
Path
objects for both the relative and absolute paths to my_file.txt
.
- We use the
exists()
method of the Path
objects to check if the file exists.
- We also check for the existence of the current directory and a non-existent file using
pathlib
.
Now, let's run the script again. Open your terminal in VS Code and execute the script using the following command:
python file_paths.py
You should see output similar to this:
The relative path 'my_file.txt' exists (using pathlib).
The absolute path /home/labex/project/my_file.txt exists (using pathlib).
The directory /home/labex/project exists (using pathlib).
The file non_existent_file.txt does not exist (using pathlib).
This demonstrates how to use pathlib.Path
as an alternative to os.path.exists()
for verifying the existence of files and directories. The pathlib
module offers a more object-oriented and often more readable way to interact with file paths in Python.