我已经设置了一个模拟操作系统的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搁置文件系统‘之外还能搜索什么,而且也得不到任何有用的东西。
发布于 2012-01-11 12:31:27
我在下面提供了一些代码来帮助您,但首先,一些总体建议应该有助于您的设计:
shelve中更改可变对象,则必须a)将写回设置为True,以及b)调用fs.sync()来设置它们。因此,我修复了cd,并为您编写了一个基本的mkdir命令。正如我在上面所说的,让它们工作的关键是让current_dir成为一个显示当前路径的列表,并且有一种简单的方法( current_dictionary函数)来从该列表转到适当的文件系统目录。
有了这些,下面是让你开始的代码:
import shelve
fs = shelve.open('filesystem.fs', writeback=True)
current_dir = []
def install(fs):
# create root and others
username = raw_input('What do you want your username to be? ')
fs[""] = {"System": {}, "Users": {username: {}}}
def current_dictionary():
"""Return a dictionary representing the files in the current directory"""
d = fs[""]
for key in current_dir:
d = d[key]
return d
def ls(args):
print 'Contents of directory', "/" + "/".join(current_dir) + ':'
for i in current_dictionary():
print i
def cd(args):
if len(args) != 1:
print "Usage: cd <directory>"
return
if args[0] == "..":
if len(current_dir) == 0:
print "Cannot go above root"
else:
current_dir.pop()
elif args[0] not in current_dictionary():
print "Directory " + args[0] + " not found"
else:
current_dir.append(args[0])
def mkdir(args):
if len(args) != 1:
print "Usage: mkdir <directory>"
return
# create an empty directory there and sync back to shelve dictionary!
d = current_dictionary()[args[0]] = {}
fs.sync()
COMMANDS = {'ls' : ls, 'cd': cd, 'mkdir': mkdir}
install(fs)
while True:
raw = raw_input('> ')
cmd = raw.split()[0]
if cmd in COMMANDS:
COMMANDS[cmd](raw.split()[1:])
#Use break instead of exit, so you will get to this point.
raw_input('Press the Enter key to shutdown...')下面是一个演示:
What do you want your username to be? David
> ls
Contents of directory /:
System
Users
> cd Users
> ls
Contents of directory /Users:
David
> cd David
> ls
Contents of directory /Users/David:
> cd ..
> ls
Contents of directory /Users:
David
> cd ..
> mkdir Other
> ls
Contents of directory /:
System
Users
Other
> cd Other
> ls
Contents of directory /Other:
> mkdir WithinOther
> ls
Contents of directory /Other:
WithinOther重要的是要注意,到目前为止,这只是一个玩具:还有一大堆工作要做。这里有几个例子:
现在只有目录这样的东西-没有常规的files.
mkdir不会检查目录是否已经存在,它会用一个空的directory.
ls Users),只检查你当前的目录。不过,这应该会向您展示一个跟踪当前目录的设计示例。祝好运!
https://stackoverflow.com/questions/8813847
复制相似问题