Python:
Checking if a directory exists
How to:
Python provides native ways to check for a directory’s existence using the os
and pathlib
modules. Here are examples for both:
Using os
module
import os
# Specify the directory path
dir_path = "/path/to/directory"
# Check if the directory exists
if os.path.isdir(dir_path):
print(f"The directory {dir_path} exists.")
else:
print(f"The directory {dir_path} does not exist.")
Using pathlib
module
from pathlib import Path
# Specify the directory path
dir_path = Path("/path/to/directory")
# Check if the directory exists
if dir_path.is_dir():
print(f"The directory {dir_path} exists.")
else:
print(f"The directory {dir_path} does not exist.")
Third-party libraries
Although Python’s standard library is sufficient for checking if a directory exists, libraries like pathlib2
can be alternatives for consistency across Python versions or additional functionality.
Note: As of the latest Python versions, pathlib
is robust enough for most use cases, making third-party libraries less necessary for this specific task.