如何使用shelve实现Python虚拟文件系统

5 投票
1 回答
6553 浏览
提问于 2025-04-17 09:58

我写了一个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'之外该搜索什么,而这个搜索也没有找到有用的东西。

1 个回答

9

下面我给你提供一些代码来帮助你,但首先,有一些整体的建议可以帮助你设计:

  • 你在更改目录时遇到困难的原因是,你对当前目录的表示方式不对。你的当前目录应该像一个列表,从顶层目录到你现在的目录。一旦你有了这个列表,你就可以根据目录来选择如何使用 shelve 存储文件(记住,shelve 中的所有键必须是字符串)。

  • 看起来你打算用一系列嵌套字典来表示文件系统——这是个不错的选择。但要注意,如果你在 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),只能查看你当前的目录。

不过,这应该能给你一个关于如何跟踪当前目录的设计示例。祝你好运!

撰写回答