-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.py
More file actions
185 lines (165 loc) · 5.14 KB
/
Commands.py
File metadata and controls
185 lines (165 loc) · 5.14 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
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
import shutil
import os
from matcher import wrongDir
def cNextDir(subItem):
i = os.getcwd()
ko = i + "/" + subItem
return ko
def alreadyHere(subItem):
if os.path.exists(subItem):
return True
else:
return False
def current():
return f"{os.getcwd()}/ "
def start():
os.chdir("/home")
#def optDir():
#return os.listdir()
#def optCmd():
#cmd = ["cd", "ls", "search", "run", "rm", "touch", "mkdir", "mv", "cp", "rmdir", "findext"]
#return cmd
#-----------------------------------------------------
def cd(item): #opens or backs out of a dir
x = item.split(" ", 1)
if len(x) > 1:
if os.path.exists(x[1]):
x = x[1]
c = str(os.getcwd())
o = [c, x]
os.chdir("/".join(o))
else:
print(f"That file doesnt exist (maybe you meant {wrongDir(x[1])})")
else:
c = str(os.getcwd()).split("/")
if len(c) >= 3:
#print(c)
c.pop(-1)
os.chdir("/".join(c))
else:
print("You cant go any further")
def ls(): #lists the current dir
print(os.listdir())
def search(item): #Just confirms if a file is there
if item != "":
if os.path.exists(item):
print(f"{item} is present")
else:
print(f"Sorry! {item} is not here! Perhaps you meant {wrongDir(item)}")
def run(item): #should run a file or even a normal command
x = item.split(" ", 1)
if len(x) == 2:
if shutil.which(x[1]) is None:
print(f"No executable found for command {x[1]}")
else:
os.system(x[1])
else:
print("No arguments found")
def rm(item): #removes a file NOT a folder
x = item.split(" ", 1)
if len(x) == 2:
n = x[1]
if os.path.exists(n):
conf = str(input(f"confirm you want to delete {n}? (y/n)"))
if conf == "yes" or conf == "y":
os.remove(n)
print(f"{n} deleted")
else:
print(f"{n} doesnt exist (maybe you meant {wrongDir(x[1])})")
else:
print("Not enough arguments")
def touch(item): #creates a file
x = item.split(" ", 1)
if len(x) == 2 and alreadyHere(x[1]) == False:
conf = str(input(f"confirm you want to create a file? (y/n)"))
if conf == "yes" or conf == "y":
x = x[1]
os.system("touch {}".format(x))
else:
print(" ")
else:
print("Not enough arguments")
def mkdir(item): # creates a folder
x = item.split(" ", 1)
conf = str(input(f"confirm you want to create a folder? (y/n)"))
if conf == "yes" or conf == "y":
if len(x) == 2 and alreadyHere(x[1]) == False:
x = x[1]
os.mkdir(x)
elif alreadyHere("Untitled") == False and len(x) == 1:
os.mkdir("Untitled")
else:
print("No file created (Name taken)")
def mv(item): # moves files
try:
x = item.split(" ", 1)
if len(x) == 2:
x = x[1]
x = x.split(" -/- ", 1)
fail = "Usage: mv source -/- destination"
st = x.pop(0)
des = x.pop(0)
#print(st)
#print(des)
if os.path.exists(st):
os.replace(st, des)
print("Files have been moved")
else:
print(f"{fail} \n Maybe you got the path wrong?")
else:
print(f"{fail} \n Maybe you didnt put enough arguments?")
except:
print("An error has occured")
def cp(item): #copies files
try:
x = item.split(" ", 1)
fail = "Usage: cp source -/- destination"
if len(x) == 2:
x = x[1]
x = x.split(" -/- ", 1)
st = x.pop(0)
des = x.pop(0)
#print(st)
#print(des)
if os.path.exists(st):
shutil.copy2(st, des)
print("Files have been copied")
else:
print(f"{fail} \n Maybe you got the path wrong?")
else:
print(f"{fail} \n Maybe you didnt put enough arguments?")
except:
print("An error has occured")
def rmdir(item): # removes folders
x = item.split(" ", 1)
n = x[1]
if os.path.exists(n):
conf = str(input(f"confirm you want to delete {n}? (y/n)"))
if conf == "yes" or conf == "y":
if len(os.listdir(cNextDir(n))) == 0:
os.rmdir(n)
print(f"{n} deleted")
else:
print(f"There may be some files inside")
else:
print(f"{n} doesnt exist (maybe you meant {wrongDir(n)}?)")
def findext(item): # finds files with given extension
try:
def ext(i):
if x.endswith(i):
l.append(x)
g = item.split(" ", 1)
if len(g) == 2:
g = g[1]
o = list(os.listdir())
l = []
if g[0] == ".":
for x in o:
ext(x)
else:
j = "." + g
for x in o:
ext(j)
print(l)
except:
print("An error has occured")