Skip to content

Commit 8fe13e4

Browse files
committed
update find_file to avoid the walk() method
1 parent 87c7363 commit 8fe13e4

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

bnd/config.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,29 @@ def _load_config() -> Config:
149149
return Config()
150150

151151

152-
def find_file(
153-
main_path: str | Path, extension: tuple[str | Path] = (".txt",)
154-
) -> list[Path]:
152+
def find_file(path: str | Path, extension: tuple[str] = ('.raw.kwd',)) -> list[Path]:
155153
"""
156-
This function finds all the file types specified by 'extension' in the 'main_path' directory
157-
and all its subdirectories and their sub-subdirectories etc.,
158-
and returns a list of all file paths
159-
'extension' is a list of desired file extensions: ['.dat','.prm']
154+
Recursively finds files with the specified extensions within the given path.
155+
`path` (str or Path): The directory in which to search for files.
156+
`extension`: A tuple of file extensions e.g., ('.dat', '.prm').
160157
"""
161-
if isinstance(main_path, str):
162-
path = Path(main_path)
163-
else:
164-
path = main_path
158+
p = Path(path)
159+
if not p.exists():
160+
raise FileNotFoundError(f"Path does not exist: {p}")
165161

162+
# Convert extension to list if it is a string.
166163
if isinstance(extension, str):
167-
extension = extension.split() # turning extension into a list with a single element
168-
169-
return [
170-
Path(walking[0] / goodfile)
171-
for walking in path.walk()
172-
for goodfile in walking[2]
173-
for ext in extension
174-
if goodfile.endswith(ext)
175-
]
164+
extension = extension.split()
165+
166+
# Normalize extensions to ensure they start with a dot.
167+
normalized_exts = [ext if ext.startswith('.') else '.' + ext for ext in extension]
168+
169+
found_files = []
170+
for ext in normalized_exts:
171+
for file in p.rglob(f"*{ext}"):
172+
if file.is_file():
173+
found_files.append(file)
174+
return found_files
176175

177176

178177
def list_dirs(main_path: str | Path) -> list[str]:

0 commit comments

Comments
 (0)