5

我已经设置了一个模拟操作系统的 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搁置文件系统”之外要搜索什么,这没有任何用处。

4

1 回答 1

9

我在下面提供了一些代码来帮助您,但首先,一些总体建议应该可以帮助您进行设计:

  • 您在更改目录时遇到困难的原因是您以错误的方式表示当前目录变量。您的当前目录应该类似于一个列表,从您的顶级目录到当前目录。一旦你有了它,你只需根据文件的目录选择如何使用搁置存储文件(考虑到搁置中的所有键都必须是字符串)。

  • 看起来您打算将文件系统表示为一系列嵌套字典 - 一个不错的选择。但请注意,如果您更改 中的可变对象shelve,您必须 a) 将 writeback 设置为 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

重要的是要注意,到目前为止,这只是一个玩具:还有很多事情要做。这里有一些例子:

  • 现在只有目录这样的东西 - 没有常规文件。

  • mkdir不检查一个目录是否已经存在,它会用一个空目录覆盖一个目录。

  • 您不能ls将特定目录作为参数(如ls Users),只能使用当前目录。

不过,这应该向您展示一个用于跟踪当前目录的设计示例。祝你好运!

于 2012-01-11T04:31:27.397 回答