Skip to content

Commit 5930f2d

Browse files
committed
feat(disk): add dir_exists() as the directory-only sibling of file_exists()
`file_exists()` wraps `os.path.isfile()` and therefore returns False for directories, which is easy to miss when reading a caller. The new `dir_exists()` makes the intent explicit and avoids the trap of silently never matching directory paths. The docstring on `file_exists` now also calls out the file-only behavior and points readers at `dir_exists`.
1 parent 7902a27 commit 5930f2d

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11-
tbd
11+
### Added
12+
13+
* disk.py: add `dir_exists()` as the directory-only counterpart to `file_exists()`. The existing `file_exists()` wraps `os.path.isfile()` and therefore returns `False` for directories, which is easy to miss; callers that want to check for a directory should now use `dir_exists()`
1214

1315

1416
## [v3.0.0] - 2026-04-13

disk.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,42 @@ def bd2dmd(device):
5555
return mapped_device if os.path.islink(mapped_device) else ''
5656

5757

58+
def dir_exists(path):
59+
"""
60+
Check if a directory exists at the given path.
61+
62+
Use this when you specifically want a directory check. `file_exists()`
63+
only returns `True` for regular files (it is `os.path.isfile()` under
64+
the hood), so passing a directory to it always returns `False`.
65+
66+
### Parameters
67+
- **path** (`str`):
68+
The path to the directory to check.
69+
70+
### Returns
71+
- **bool**:
72+
`True` if the path exists and is a directory (or a symlink to one),
73+
`False` otherwise.
74+
75+
### Example
76+
>>> dir_exists('/etc')
77+
True
78+
>>> dir_exists('/etc/passwd')
79+
False
80+
>>> dir_exists('/path/that/does/not/exist')
81+
False
82+
"""
83+
return os.path.isdir(path)
84+
85+
5886
def file_exists(path, allow_empty=False):
5987
"""
60-
Check if a file exists at the given path, optionally allowing empty files.
88+
Check if a regular file exists at the given path, optionally allowing
89+
empty files.
90+
91+
This wraps `os.path.isfile()`, so it only matches regular files and
92+
symlinks pointing at regular files. Directories return `False`; use
93+
`dir_exists()` for directory checks.
6194
6295
### Parameters
6396
- **path** (`str`):

0 commit comments

Comments
 (0)