-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateElementList.py
53 lines (48 loc) · 1.93 KB
/
CreateElementList.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
"""
this a class that handle all operations of get all items in specific folders that user enter
"""
import os
class CreateElementList:
__allElements = []
__order = ""
def __init__(self, order):
self.__order = order
# this function takes directory name and list all elements in it, if the elements is has a folder also
# then it will list all elements in this folder and push all items in this folder in the list
# if the folder has also another folder
def __allItemsInDirs(self, dirName, counter = 0):
itemsInFolders = []
if counter >= 9:
print("You Enter Location of Folder That Has more than 8 folders one inside other, Please Enter a Specific folder from this")
return None
else:
try:
for item in os.listdir(dirName):
item_path = (dirName + "\\" + item)
if os.path.isdir(item_path):
counter += 1
itemsInFolders += self.__allItemsInDirs(item_path, counter)
else:
itemsInFolders.append(item_path)
return itemsInFolders
except FileNotFoundError:
print("Folder Not Found")
return itemsInFolders
except:
print("There's an Error")
return itemsInFolders
def listAllElementsInDirs(self):
print(f"Now Enter Your Locations One After One, and If You Finsh The {self.__order} Folders Press s to Stop")
counter = 0
while True:
location = input(f"Enter Location No.{counter + 1} or s to Stop : ")
counter += 1
if location != 's':
lis = self.__allItemsInDirs(location)
if lis is not None:
self.__allElements += lis
else:
break
else:
break
return lis