How do I check if a directory exists in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Checking whether a directory exists before reading from it, writing to it, or creating it is a fundamental file system operation in Python. The standard approaches use os.path.isdir() for a simple boolean check and pathlib.Path.is_dir() for a modern, object-oriented approach. Understanding the difference between checking for existence and checking for directories (vs files) prevents subtle bugs.
Method 1: os.path.isdir()
Returns True if the path exists and is a directory:
Method 2: pathlib.Path.is_dir() (Recommended)
pathlib (Python 3.4+) provides an object-oriented approach:
Method 3: os.path.exists() (Checks Any Path)
os.path.exists() returns True for both files and directories:
Use isdir() when you specifically need a directory, and exists() when any path type is acceptable.
Comparison of Methods
| Method | Checks | Returns True for files | Follows symlinks |
os.path.isdir() | Directory only | No | Yes |
os.path.exists() | Any path | Yes | Yes |
Path.is_dir() | Directory only | No | Yes |
Path.exists() | Any path | Yes | Yes |
Create Directory If Not Exists
Checking Permissions
A directory may exist but not be readable or writable:
Handling Symlinks
By default, both isdir() and is_dir() follow symbolic links:
EAFP vs LBYL
Python encourages "Easier to Ask Forgiveness than Permission" (EAFP) over "Look Before You Leap" (LBYL). Instead of checking existence first, try the operation and catch the exception:
EAFP is preferred when multiple processes or threads might modify the filesystem concurrently.
Common Pitfalls
os.path.exists()returns True for files too: If you need to verify something is specifically a directory (not a file), useisdir()instead ofexists().exists()returns True for both.- Race conditions (TOCTOU): Between checking
isdir()and using the directory, another process can delete it. Use EAFP with try/except for robust code. - Relative paths:
os.path.isdir('data')checks relative to the current working directory, which may differ from where your script is located. UsePath(__file__).parent / 'data'for paths relative to the script. - Trailing slashes:
os.path.isdir('/path/to/file/')returns False even if/path/to/fileexists as a file. The trailing slash does not make it a directory check — it just means the path lookup fails. - Broken symlinks:
isdir()returns False for broken symlinks (where the target does not exist). Useos.path.islink()to check for the symlink itself.
Summary
- Use
os.path.isdir(path)to check if a path is an existing directory - Use
Path(path).is_dir()for the modern pathlib approach - Use
os.makedirs(path, exist_ok=True)to create a directory (with parents) only if it does not exist - Prefer EAFP (try/except) over LBYL (check-then-act) to avoid race conditions
- Use
isdir()instead ofexists()when you specifically need a directory, not just any existing path
Related reading
- How do I check if a directory exists in Python?
- How do I check if a given Python string is a substring of another one?
- How do I check if a list is empty?
- How do I check if a pandas DataFrame is empty?
- How do I check if a string is valid JSON in Python?
- How do I check if a string represents a number float or int?
- How do I check if I'm running on Windows in Python?
- How do I check that multiple keys are in a dict in a single pass?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.