-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_scanner.py
More file actions
45 lines (34 loc) · 1.07 KB
/
files_scanner.py
File metadata and controls
45 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Iterable
import os
def iter_files(directory: str) -> Iterable[str]:
"""
Lazily iterate over all files within a directory.
Only regular files are yielded. Subdirectories and other
non-file entries are ignored.
Args:
directory:
Path to the directory to scan.
Yields:
str:
Absolute or relative file paths contained in the
specified directory.
Raises:
FileNotFoundError:
If the specified directory does not exist.
NotADirectoryError:
If the provided path is not a directory.
PermissionError:
If the program lacks permission to access
the directory.
"""
if not os.path.exists(directory):
raise FileNotFoundError(
f"Directory does not exist: {directory}"
)
if not os.path.isdir(directory):
raise NotADirectoryError(
f"Path is not a directory: {directory}"
)
for entry in os.scandir(directory):
if entry.is_file():
yield entry.path