-
Notifications
You must be signed in to change notification settings - Fork 0
/
unixSim.py
111 lines (94 loc) · 2.58 KB
/
unixSim.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
# Possible commands:
# mkdir <filename>, touch <filename>, cd <path>, ls or ls <path>, pwd, rmdir <path>, exit(d)
import os
#done
def mkdir(s):
try:
os.makedirs(s)
print(s + " directory created successfully\n")
except IOError:
print("Error making folder" + s)
#done
def touch(s):
try:
outputfile = open(s,"w")
print("File " + s + " created successfully")
except IOError:
print("there was an error creating output file!")
#done
def pwd():
print(os.getcwd())
#done
def cd(s):
try:
os.chdir(s)
print("navigated to " + s + " successfully")
except IOError:
print("there was an error navigating to " + s)
#done
def ls(s):
if s == "":
s = os.getcwd()
try:
print(os.listdir(s))
except IOError:
print("there was an error navigating to " + s)
#done
def rmdir(s):
print(s)
try:
os.removedirs(s)
except IOError:
print("there was an error navigating to " + s)
def userSelection():
flag = 0
print("Welcome to Unix File System Simulator\n ")
while flag is 0:
print("Menu options:")
print("mkdir")
print("touch")
print("cd")
print("ls")
print("pwd")
print("rmdir")
print("exit")
selection = input("input: ")
r = selection.split(" ")
if len(r) < 2 and selection != "pwd" and selection != "ls" and selection != "exit":
print("invalid input!")
flag = 0
continue
searchVal = r[0]
if(selection != "pwd" and selection != "ls" and selection != "exit"):
passVal = r[1]
else:
passVal = ""
if(searchVal.lower() == ("mkdir")):
mkdir(passVal)
flag = 0
elif(searchVal.lower() == ("touch")):
touch(passVal)
flag = 0
elif (searchVal.lower() == ("cd")):
cd(passVal)
searchVal = passVal
flag = 0
elif (searchVal.lower() == ("ls")):
ls(passVal)
flag = 0
elif (searchVal.lower() == ("pwd")):
pwd()
flag = 0
elif (searchVal.lower() == ("rmdir")):
rmdir(passVal)
flag = 0
elif (searchVal.lower() == "exit"):
flag = 1
print("exiting..")
break
else:
print("unsupported command")
flag = 0
def main():
userSelection()
main()