-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindpath.py
More file actions
104 lines (86 loc) · 2.85 KB
/
findpath.py
File metadata and controls
104 lines (86 loc) · 2.85 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
class FindPath:
def __init__(self, root=None):
self.root = root
self.root_path = None
self.all_paths = None
self.file = None
self.your_path = None
self.current_dir = os.getcwd()
def find(self, desired_file: str) -> str:
self.file = desired_file
if self.root == "/":
self.root_path = self.root
while True:
if os.path.basename(self.current_dir) == self.root:
self.root_path = self.current_dir
break
elif self.current_dir == "/":
raise FileNotFoundError("Requested directory not found")
os.chdir("..")
FindPath.find_all_paths(self, self.root_path)
desired_path = ""
occurrences = 0
for path in self.all_paths:
if self.file == os.path.basename(path):
desired_path = path
occurrences += 1
if not desired_path:
raise FileNotFoundError("Requested file not found")
elif occurrences > 1:
raise ValueError("More than one file found")
self.your_path = desired_path
return self.your_path
def find_all_paths(self, root_path: str) -> list:
paths = []
current_dir = root_path
current_dir_contents = os.listdir(current_dir)
for path in current_dir_contents:
if not os.path.isdir(os.path.join(current_dir, path)):
paths.append(os.path.join(current_dir, path))
else:
paths.extend(
FindPath.find_all_paths(self, os.path.join(current_dir, path))
)
self.all_paths = paths
return self.all_paths
@property
def root(self):
if self._root is None:
raise AttributeError("Root is not set")
return self._root
@root.setter
def root(self, value):
self._root = value
@property
def root_path(self):
if self._root_path is None:
raise AttributeError("Root is not set")
return self._root_path
@root_path.setter
def root_path(self, value):
self._root_path = value
@property
def all_paths(self):
if self._all_paths is None:
raise AttributeError("Root is not set")
return self._all_paths
@all_paths.setter
def all_paths(self, value):
self._all_paths = value
@property
def file(self):
if self._file is None:
raise AttributeError("File is not set")
return self._file
@file.setter
def file(self, value):
self._file = value
@property
def your_path(self):
if self._your_path is None:
raise AttributeError("Your path is not set")
return self._your_path
@your_path.setter
def your_path(self, value):
self._your_path = value