-
Notifications
You must be signed in to change notification settings - Fork 29
/
search_directory.py
98 lines (74 loc) · 2.9 KB
/
search_directory.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
import ast
import os
import stat
import string
import tempfile
from ctypes import windll
temp_dir = tempfile.gettempdir()
file_system_dict = {}
def get_dup_key(key,keys):
count = 0
for k in keys:
if key in k:
count+=1
return count
def listdir_nohidden(path):
return bool(os.stat(path).st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN)
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.ascii_uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
def find():
# # print('{} is a {}'.format(name, type))
# file_dict = {}
# dir_dict = {}
# rex = re.compile(name.lower())
winpath = os.environ['WINDIR'].split(':')[0]
print(winpath)
# drives = get_drives(
drives = ['D']
for drive in drives:
# drive='D'
print(drive)
for root, dirs, files in os.walk(drive + ':\\Akhilesh\\',):
# if len(root)==3:
for f in files:
# print os.path.join(root, f.lower())
# result = rex.search(f.lower())
# if result:
if listdir_nohidden(os.path.join(root, f))==False and temp_dir not in f and 'Recycle' not in f and '__' not in f:
key = os.path.join(root, f).rsplit('\\')[-1]
# print(key, os.path.join(root, f))
file_system_dict.update({key+'_{}'.format(get_dup_key(key,file_system_dict.keys())):os.path.join(root, f)})
else:
pass# print('Hidden')
for dir in dirs:
# print os.path.join(root, dir.lower())
# result = rex.search(dir.lower())
# if result:
if listdir_nohidden(os.path.join(root, dir))==False and temp_dir not in dir and 'Recycle' not in dir and '__' not in dir:
key = os.path.join(root, dir).split('\\')[-1].lower().split('.')[0]
# print (key, os.path.join(root, dir))
file_system_dict.update({key+'_{}'.format(get_dup_key(key,file_system_dict.keys())): os.path.join(root, dir)})
# dir_dict.update({'ghost_{}'.format(get_dup_key(key,dir_dict.keys())): 'rider'})
# if you want to find only one
else:
pass
# print('Hidden')
# else:
# break
return file_system_dict
file_name='dict.txt'
if os.path.exists(file_name):
with open(file_name, 'r') as f:
file_system_dict = ast.literal_eval(f.read())
f.close()
else:
with open(file_name, 'w') as f:
print('File System dictionary not found. Wait, i am doing so for the search functionality.')
f.write(str(find()))
f.close()