我已经建立了一个模拟操作系统的Python脚本。它有一个命令提示符和一个虚拟文件系统。我使用shelve模块来模拟文件系统,它是多维的,以便支持目录的层次结构。但是,我在执行“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 shelve file system”之外要搜索什么,这没有任何有用的东西。