我已经设置了一个模拟OS的Python脚本。它有一个命令提示符和一个虚拟文件系统。我正在使用搁置模块来模拟文件系统,它是多维的,以支持目录层次结构。但是,我无法执行'cd'命令。我不知道如何进入和退出目录,即使我在第一次启动程序时创建了一小组目录。这里是我的代码:如何使用shelve实现Python虚拟文件系统
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...')
我没有得到一个错误,我只是不知道如何做到这一点并没有什么搜索除了“蟒蛇货架文件系统”的想法,而且不得到有用的东西。
有趣!你介意分享这是为了什么吗? – 2012-01-11 03:43:08
我设置了你的代码在我的Eclipse中为python3.x工作...现在就玩它。到目前为止,我有点困惑。但没关系。正如大卫所说,你能提供一些背景吗? – Bry6n 2012-01-11 03:57:34
这是我可以试试看。只是为了好玩。 – elijaheac 2012-01-11 22:25:30