-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcherche_path_SLOW.py
204 lines (177 loc) · 6.29 KB
/
cherche_path_SLOW.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from pathlib import Path
import os
import re
import sys
import locale
import time
def Clean():
os.system('cls' if os.name == 'nt' else 'clear')
# if os.name == 'nt':
# print("nt")
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def erreur():
print(bcolors.FAIL + "usage : trouve.py <repIn> <regex comme \"^\..*\" (les .DS_Store par exemple)>" + bcolors.ENDC)
print(bcolors.FAIL +
"usage : trouve.py <repIn> <regex comme \".*\" (tout)>" + bcolors.ENDC)
print(bcolors.FAIL + "usage : trouve.py <repIn> <regex comme \".*\.ps1$\" (fichiers se terminant par .ps1)>" + bcolors.ENDC)
print(bcolors.FAIL + "usage : trouve.py <repIn> <regex comme \".*\.py$\" (fichiers se terminant par .py)>" + bcolors.ENDC)
print(bcolors.FAIL +
"usage : trouve.py <repIn> <regex comme \"applica.*Data\">" + bcolors.ENDC)
print(bcolors.FAIL + """
^ start
$ end
\. le point
.* n'importe quel caractère
""" + bcolors.ENDC)
sys.exit(1)
def lireListe(s, l):
if l != []:
a = 10
if len(l) > a:
print(s, *l[0:a], sep="\n")
else:
print(s, *l, sep="\n")
def regex_trouvé(regex, s):
# print("regex", dirname)
r = re.search(regex, s, re.IGNORECASE) # https://regex101.com/
if r:
# print("ok", s)
return True
return False
def est_inclus_dans(dir_exclus, nom):
for i in dir_exclus:
# print("i", i, nom)
if regex_trouvé(i, nom):
return True
return False
Clean()
# Get the list of user's
# environment variables
env_var = os.environ
import pprint
# Print the list of user's
# environment variables
# print("User's Environment variable:")
# pprint.pprint(dict(env_var), width = 1)#https://www.geeksforgeeks.org/python-os-environ-object/
print('version', sys.version)
if "HOME" in os.environ:
print('user', os.environ['HOME']) # sur Unix
elif "USERPROFILE" in os.environ:
print('USERPROFILE', os.environ['USERPROFILE']) # sur windows
print('getfilesystemencoding', sys.getfilesystemencoding())
print('locale.getpreferredencoding', locale.getpreferredencoding())
print('écrire chinois traditionel : 漢字')
if len(sys.argv) != 3:
erreur()
else:
start = time.perf_counter()
nmPgm = sys.argv[0]
dir = sys.argv[1]
regex = sys.argv[2]
print(bcolors.HEADER + "wait..." + bcolors.ENDC)
# TEST
# user = os.environ['USERPROFILE'] # on windows
# p = Path(user + "/Documents/")
# p = Path(user + "/Documents/Mes vidéos")
# p = Path(user + "/Documents/é é")
# regex = ".*musi.*"
dir_exclus = ["AppData", "documents_"]
# dir_exclus = []
p = Path(dir)
stack = []
execp = []
dirs = []
files = []
others = []
liens = []
n = 0
if p.is_dir():
# print("1")
if not est_inclus_dans(dir_exclus, p.name):
# print('NON dedans')
stack = [p]
if regex_trouvé(regex, p.name):
dirs.append(p)
# print("ok")
# else:
# print('dedans')
elif p.is_file():
if regex_trouvé(regex, p.name):
files.append(p)
print('stack', stack)
print('files', files)
print('dirs', dirs)
# exit(0)
while stack:
path = stack.pop()
# print('path', path)
# exit(0)
n += 1
try:
for e in path.iterdir():
if e.is_dir():
nom = e.name
if not est_inclus_dans(dir_exclus, nom):
stack.append(e)
if regex_trouvé(regex, nom):
dirs.append(e)
elif e.is_file():
nom = e.name
if regex_trouvé(regex, nom):
files.append(e)
elif e.is_symlink():
liens.append(e)
else:
others.append(e)
except PermissionError:
print(bcolors.FAIL + "PermissionError" + " " + str(path) + bcolors.ENDC)
except NotADirectoryError:
print(bcolors.FAIL + "NotADirectoryError" + " " + str(path) + bcolors.ENDC)
# ...
except:
print(bcolors.FAIL + "except" + " " + str(path) + bcolors.ENDC)
# print("execp", *execp, sep="\n")
# print("dirs", *dirs, sep="\n")
# print("files", *files, sep="\n")
# print("liens", *liens, sep="\n")
# print("others", *others, sep="\n")
# print("end")
i = 1
for s in files:
# s = s[ : 2]+"/"+s[2 : ]
print(bcolors.OKGREEN + str(i) + " " + str(s) + bcolors.ENDC)
i += 1
i = 1
for s in dirs:
print(bcolors.OKBLUE + str(i) + " " + str(s) + bcolors.ENDC)
i += 1
end = time.perf_counter()
ms = (end-start) # * 10**6
print(f"Elapsed {ms:.03f} secs.")
print(n, "élément scannés")
# In Windows 11, in `C:\Users\$env:username\Documents` there are special directories that you haven't access.
# You can list them with PowerShell command line:
# Get-ChildItem -Path C:\Users\$env:username\Documents -force
# Get-ChildItem -Path C:\Users\$env:username\Documents -force
# ...
# l--hs 29/01/2023 15:32 My music -> C:\Users\****\Music
# l--hs 29/01/2023 15:32 My pictures -> C:\Users\****\Pictures
# l--hs 29/01/2023 15:32 My videos -> C:\Users\****\Videos
# pathlib new in 3.4
# ... "pathlib" module providing object-oriented filesystem paths
# https://www.python.org/downloads/release/python-340
# intéressant : demander pardon avec try que de faire pleins de tests et qu'un autre thread (par ex) supprime la ressource testée
# https://stackoverflow.com/a/28015065
# https://stackoverflow.com/questions/58306849/python-having-trouble-opening-a-file-with-spaces
# https://docs.python.org/2/glossary.html#term-lbyl
# exemple
# cls;cls; python .\cherche_path.py.py C:\Users\***\ .*musi.*