-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
57 lines (48 loc) · 1.9 KB
/
search.py
File metadata and controls
57 lines (48 loc) · 1.9 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
#!/usr/bin/env python3
import os
print(r"""
_____ _ _____ _ _
/ ____| | | / ____| (_) | |
| (___ ___ __ _ _ __ ___| |__ | (___ ___ _ __ _ _ __ | |_
\___ \ / _ \/ _` | '__/ __| '_ \ \___ \ / __| '__| | '_ \| __|
____) | __/ (_| | | | (__| | | | ____) | (__| | | | |_) | |_
|_____/ \___|\__,_|_| \___|_| |_| |_____/ \___|_| |_| .__/ \__|
| |
|_|
""")
path = input("Where do you want to search?: ").strip()
kind = input("Are you looking for a file or a directory? (file/directory): ").strip().lower()
name = input("What name are you looking for?: ").strip()
match = input("Should the name match exactly or just contain it? (exact/contain): ").strip().lower()
if kind not in ("file", "directory"):
print("Oops! Please enter either 'file' or 'directory'.")
exit(1)
elif match not in ("exact", "contain"):
print("Please type 'exact' or 'contain' to specify the match type.")
exit(1)
elif not os.path.isdir(path):
print(f"Sorry, the path '{path}' doesn't exist or is not a directory.")
exit(1)
try:
os.listdir(path)
except Exception as e:
print(f"Can't access path: {e}")
exit(1)
results = []
target = name.lower()
for root, dirs, files in os.walk(path):
items = files if kind == "file" else dirs
for item in items:
check = os.path.splitext(item)[0] if kind == "file" else item
check = check.lower()
if match == "exact":
if check == target:
results.append(os.path.join(root, item))
elif target in check:
results.append(os.path.join(root, item))
if results:
print("\nMatches found:")
for r in results:
print(r)
else:
print("\nNo matches found. Maybe try a different name or path?")