Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:14:19+00:00 2026-05-28T01:14:19+00:00

I have set up a Python script that simulates an OS. It has a

  • 0

I have set up a Python script that simulates an OS. It has a command prompt and a virtual file system. I am using the shelve module to simulate the file system, being multi-dimensional in order to support a hierarchy of directories. However, I am having trouble implementing a ‘cd’ command. I don’t know how to get in and out of directories, even though I have a small set of directories created when you first launch the program. Here is my code:

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...')

I’m not getting an error, I just have no idea how to do it and no idea of what to search for besides ‘python shelve file system’, and that doesn’t get anything useful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:14:20+00:00Added an answer on May 28, 2026 at 1:14 am

    I provide some code to help you out below, but first, some overall advice that should help you with your design:

    • The reason you’re having difficulty with changing directories is that you are representing the current directory variable the wrong way. Your current directory should be something like a list, from your top level directory to your current one. Once you have that, you just make a choice about how store files using shelve based on their directory (considering that all keys in Shelve must be strings).

    • It looks like you were planning on representing the filesystem as a series of nested dictionaries- a good choice. But note that if you change mutable objects in shelve, you have to a) set writeback to True and b) call fs.sync() to set them.

    • You should be structuring your entire filesystem in a class rather than in a series of functions. It will help you keep your shared data organized. The below code doesn’t follow that but it is worth thinking about.

    So, I fixed up cd and also wrote a rudimentary mkdir command for you. The critical thing for making them work is to, as I said above, have current_dir be a list that shows your current path, and also to have an easy way (the current_dictionary function) to get from that list to the appropriate filesystem directory.

    With that, here’s the code to get you started:

    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...')
    

    And here’s a demonstration:

    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
    

    It’s important to note that this is so far just a toy: there is still a ton left to do. Here are a few examples:

    • Right now there is only such a thing as directories- no regular files.

    • mkdir doesn’t check if a directory already exists, it would overwrite one with an empty directory.

    • You can’t ls with a specific directory as an argument, (like ls Users), only your current directory.

    Still, this should show you an example of a design for keeping track of your current directory. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that analyzes a set of error messages and checks
I have a python logger set up, using python's logging module. I want to
I have an email account set up that triggers a python script whenever it
I have a Python script that loads a web page using urllib2.urlopen , does
I have a Python regular expression that matches a set of filenames. How to
I have created a Python file to generate a Mandelbrot set image. The original
I have set up a version control system using TortoiseSVN at my home to
I have a python script that reads raw movie text files into an sqlite
I have a PHP script that call a python script (someone else I can't
I have a simple python script that updates that statuses of justin.tv streams in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.