-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_scandir.py
More file actions
80 lines (69 loc) · 3.88 KB
/
recursive_scandir.py
File metadata and controls
80 lines (69 loc) · 3.88 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import logging
import yaml
def recursive_scandir(path: str, config_file_path: str, recursion_limit: int=2, current_recurs_step: int=1):
"""
This generator recursively scans a directory for files and nested directories.
Arguments:
---------
path: str
path-like object, the path of the root directory to be scanned.
recursion_limit: int, int=2
the depth of recursion for nested directories. This limit is used for visualization,
by default only 2nd nested directory is scanned.
value equal to 0 can be used to turn off the recursion_limit.
current_recurs_step: int=1
counter of recursion deepness used to cut off the scanning of deeper directories.
config_file_path: str
path-object containing the path to the configuration yaml file.
Yields:
------
object: object type
fetched directory or file object from a given root directory.
next(first_object): object type
the first fetched object inside a nested directory. This allows visualizing the parent directory as a container
in tkinter.treeview.
"""
with open(config_file_path, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
# Handle empty folders or denied access with try-except statement
# The dir_availability switch is used to simplify the code, as further another try-except is applied for child objects
try:
# For a correct visualization, all the necessary objects (directories and files)
# should be grouped by: 1. Type (directories-> files); 2. Name (A-Z)
sorted_scandir = sorted(os.scandir(path), key = lambda obj:(obj.is_file(), obj.name.casefold()))
dir_availability = True
except PermissionError as permission_err:
logging.basicConfig(**config['logger'])
logging.error(permission_err)
dir_availability = False
pass
if dir_availability:
for object in sorted_scandir:
# If the object inside the parent directory is unreachable, update the logfile and pass to the next object
try:
if object.is_dir(follow_symlinks=False):
yield object
current_recurs_step += 1
# Continue to scan children if the recursion counter allows it.
if current_recurs_step < recursion_limit or recursion_limit == 0:
yield from recursive_scandir(object.path,
config_file_path,
recursion_limit=recursion_limit,
current_recurs_step=current_recurs_step)
# The recursion counter must be reset to 1 to yield the rest of the root directory
current_recurs_step = 1
else:
# For nested directories, yield only the first object.
# This crucially increases the performance
first_object = os.scandir(object.path)
try:
yield next(first_object)
except:
pass
else:
yield object
except PermissionError as permission_err:
logging.basicConfig(**config['logger'])
logging.error(permission_err)
pass