我已经设置了一个模拟操作系统的 Python 脚本。它有一个命令提示符和一个虚拟文件系统。我正在使用搁置模块来模拟文件系统,它是多维的,以支持目录层次结构。但是,我在执行“cd”命令时遇到了麻烦。我不知道如何进出目录,即使我在您第一次启动程序时创建了一小部分目录。这是我的代码:
import shelve
fs = shelve.open('filesystem.fs')
directory = 'root'
raw_dir = None
est_dir = None
def install(fs):
fs['System'] = {}
fs['Users'] = {}
username = raw_input('What do you want your username to be? ')
fs['Users'][username] = {}
try:
test = fs['runbefore']
del test
except:
fs['runbefore'] = None
install(fs)
def ls(args):
print 'Contents of directory', directory + ':'
if raw_dir:
for i in fs[raw_dir[0]][raw_dir[1]][raw_dir[2]][raw_dir[3]]:
print i
else:
for i in fs:
print i
def cd(args):
if len(args.split()) > 1:
if args.split()[1] == '..':
if raw_dir[3]:
raw_dir[3] = 0
elif raw_dir[2]:
raw_dir[2] = 0
elif raw_dir[1]:
raw_dir[1] = 0
else:
print "cd : cannot go above root"
COMMANDS = {'ls' : ls}
while True:
raw = raw_input('> ')
cmd = raw.split()[0]
if cmd in COMMANDS:
COMMANDS[cmd](raw)
#Use break instead of exit, so you will get to this point.
raw_input('Press the Enter key to shutdown...')
我没有收到错误,我只是不知道该怎么做,也不知道除了“python搁置文件系统”之外要搜索什么,这没有任何用处。