-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDOS.py
139 lines (113 loc) · 4.26 KB
/
MyDOS.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
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
import os
from time import localtime
def txtFileTime(fPath):
retTime = localtime(max(min(2145916800,os.stat(fPath)[9]),946684800))
fTime = []
fTime.append(retTime[1])
fTime.append(retTime[2])
fTime.append(retTime[0])
fTime.append(retTime[3])
fTime.append(retTime[4])
return(fTime)
cmd = ""
os.chdir("/")
while (1 == 1):
cmdLine = input("\n"+os.getcwd()+">")
args = cmdLine.split(" ")
cmd = args[0].upper()
if cmd == "DIR":
nDirs = 2
nFiles = 0
print("Directory of",os.getcwd()+".")
print("."+" "*23+"<DIR>")
print(".."+" "*22+"<DIR>")
for dir in os.listdir():
if os.stat(dir)[0] & (2**15)== 0:
fTime = txtFileTime(dir)
print(dir+" "*(24-len(dir))+"<DIR>"+" "*18+"%2.2i-%2.2i-%4.4i %2.2i:%2.2i" % (fTime[0], fTime[1], fTime[2], fTime[3], fTime[4]))
nDirs += 1
tFSize = 0
tmpDir = os.getcwd()
try:
availDisk = os.statvfs(tmpDir)[1]*os.statvfs(tmpDir)[4]
except:
availDisk = 0
for dir in os.listdir():
if os.stat(dir)[0] & (2**15) != 0:
fSize = str(os.stat(dir)[6])
tFSize += os.stat(dir)[6]
fTime = txtFileTime(dir)
print(dir+" "*(35-len(dir)+10-len(fSize)),fSize,"%2.2i-%2.2i-%4.4i %2.2i:%2.2i" % (fTime[0], fTime[1], fTime[2], fTime[3], fTime[4]))
nFiles += 1
print(" "*(4-len(str(nFiles))),nFiles,"File(s)"+" "*(32-len(str(tFSize))),tFSize,"Bytes.")
print(" "*(4-len(str(nDirs))),nDirs,"Dir(s)"+" "*(33-len(str(availDisk))),availDisk,"Bytes free.")
elif cmd == "RENAME" or cmd == "REN":
if args[1] in os.listdir() and len(args) == 3:
os.rename(args[1],args[2])
elif len(args) != 3:
print("Wrong number of arguments")
else:
print("No such file:",args[1])
elif cmd == "DEL":
if len(args) == 2:
if args[1] in os.listdir():
os.remove(args[1])
else:
print("Unable to delete: "+args[1]+". File not found.")
else:
print("Illeagal Path.")
elif cmd == "TYPE":
if args[1] in os.listdir():
f = open(args[1])
print(f.read())
f.close()
else:
print("file "+args[1]+" not found.")
elif cmd == "CD":
if len(args) == 1:
print(os.getcwd())
else:
pathDirs = args[1].split("/")
savDir = os.getcwd()
goodPath = True
for path in pathDirs:
if path == "":
os.chdir("/")
elif path == "." or path == "..":
os.chdir(path)
elif path in os.listdir() and (os.stat(path)[0] & (2**15) == 0):
os.chdir(path)
else:
goodPath = False
break
if goodPath == False:
print("Unable to change to: "+args[1]+".")
os.chdir(savDir)
elif cmd == "COPY":
if args[1] in os.listdir() and args[2] not in os.listdir() and len(args) == 3:
fOrig = open(args[1])
fCopy = open(args[2], "w")
fCopy.write(fOrig.read())
fOrig.close()
fCopy.close()
elif len(args) != 3:
print("Wrong number of arguments")
elif args[1] not in os.listdir():
print("No such file:",args[1])
else:
print("File already exists:",args[2])
elif cmd == "EXIT":
break
elif cmd == "":
continue
else:
if args[0] in os.listdir() and os.stat(args[0])[0] & (2**15)!= 0 and ((args[0].split("."))[1]).upper() == "PY":
# __import__((args[0].split("."))[0])
# try:
exec(open(args[0]).read())
# except SyntaxError:
# print("A syntax error was detected in",args[0])
# except:
# print("An exception occurred in the",args[0],"python script")
else:
print("Illegal command:",args[0]+".")