forked from levietanh0001/pp2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.shell.py
38 lines (30 loc) · 1.02 KB
/
6.shell.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
import os
import subprocess
class Shell:
def __init__(self):
while True:
command = input("> ")
if command == "exit":
break
elif command[:3] == "cd ":
self.cd(command[3:])
elif command == "help":
self.psh_help()
else:
self.execute_command(command)
def execute_command(self, command):
try:
subprocess.run(command)
except FileNotFoundError:
print("'{}' is not recognized as an internal or external "
"command,operable program or batch file".format(command))
def cd(self, path):
try:
os.chdir(os.path.abspath(path))
except FileNotFoundError:
print("'{}' is not recognized as an internal or external "
"command,operable program or batch file".format(path))
def psh_help(self):
print("Basic shell implement some basic command")
if __name__ == '__main__':
shell = Shell()