-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalking.py
83 lines (73 loc) · 2.33 KB
/
walking.py
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
81
82
83
# Import the necessary modules
import os
import copy
import win32api
# Create the form of each element
WALKS = {
'Name':"", # Name of the file or folder
'Path':"", # Path of the file or folder
'Size':None, # Size of the file or folder
'Step':0, # Step of the walk
'Content' : [] # all elements in the folder (uniquely for folders)
}
def GetDrives() -> list:
"""
Get all drives of the computer
Returns:
list: List of the names of the drives
"""
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
return drives
def defineSize(list_of_elements)->int:
"""
Define the size of the folder
Args:
list_of_elements (class): List of the elements in the folder
Returns:
int: sum of the size of all elements in the folder
"""
size = 0
for element in list_of_elements:
size += element['Size']
return size
def walking(path:str, step:int=0):
"""_summary_
Args:
path (_type_): _description_
step (int, optional): _description_. Defaults to 0.
Returns:
_type_: _description_
"""
head = copy.deepcopy(WALKS)
metadata = []
head['Name'] = path
head['Path'] = path
head['Step'] = step
head['Content'] = []
try:
files = os.listdir(path)
except PermissionError as e:
print(f"Unable to search here : {path} : {e}")
head['Size'] = 0
return head
for index, element in enumerate(files):
metadata.append(copy.deepcopy(WALKS))
itexist = os.path.exists(f"{path}/{element}")
if not itexist:
metadata[index]['Name'] = element
metadata[index]['Path'] = f"{path}/{element}"
metadata[index]['Size'] = 0
metadata[index]['Step'] = step+1
else:
isFile = os.path.isfile(f"{path}/{element}")
if isFile:
metadata[index]['Name'] = element
metadata[index]['Path'] = f"{path}/{element}"
metadata[index]['Size'] = os.path.getsize(f"{path}/{element}")
metadata[index]['Step'] = step+1
else:
metadata[index] = walking(f"{path}/{element}", step+1)
head['Content'] = metadata
head['Size'] = defineSize(metadata)
return head