forked from SidharthMudgil/mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-explorer.py
More file actions
112 lines (89 loc) · 2.86 KB
/
Copy pathfile-explorer.py
File metadata and controls
112 lines (89 loc) · 2.86 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
105
106
107
108
109
110
111
112
import os
import shutil
COMMANDS = ('open', 'mv', 'cp', 'new', 'del', 'up', 'exit')
def menu():
print("open : open 'path'")
print("new : new 'path'")
print("del : del 'path'")
print("mv : mv 'from' 'to'")
print("cp : cp 'from' 'to'")
print("up : up")
print("exit : exit")
print('*' * 50)
curr_dir = os.getcwd()
for files in os.listdir(curr_dir):
print(files)
print('*' * 50)
def clean_input():
_input = input('>').strip().lower()
while " " in _input:
_input = _input.replace(" ", ' ')
query = _input.split(' ')
if query[0] in COMMANDS:
return query
print(f"command {query[0]} not found")
def file_explorer(pathway=[]):
os.system('cls')
if len(pathway) == 1:
location = pathway[0]
os.chdir(location)
menu()
query = clean_input()
if query[0] == 'exit':
return
elif query[0] == 'up':
if len(pathway) == 0:
return
else:
pathway.pop()
file_explorer(pathway)
if len(query) == 2:
command = query[0]
path = query[1].strip().strip('"')
if command == 'open':
if os.path.exists(path):
if os.path.isfile(path):
with open(path) as file:
for line in file:
print(line)
elif os.path.isdir(path):
pathway.append(path)
file_explorer(pathway)
else:
print("Path does not exist")
elif command == 'new':
if not os.path.exists(path):
if '.' in os.path.basename(path):
with open(path, 'w') as file:
pass
else:
os.mkdir(path)
else:
print("Path already exists")
elif command == 'del':
if os.path.exists(path):
if '.' in os.path.basename(path):
os.remove(path)
else:
shutil.rmtree(path)
else:
print("Path does not exist")
elif len(query) == 3:
command = query[0]
path1 = query[1].strip().strip('"')
path2 = query[2].strip().strip('"')
if command == 'cp':
if os.path.exists(path1) and os.path.exists(path2):
if '.' in os.path.basename(path1):
shutil.copy(path1, path2)
else:
shutil.copytree(path1, path2)
else:
print("Either or both paths do not exist")
elif command == 'mv':
if os.path.exists(path1) and os.path.exists(path2):
shutil.move(path1, path2)
else:
print("Either or both paths do not exist")
file_explorer(pathway)
file_explorer()