Write A Python Program For Creating Virtual File System on Linux Environment.
============ Python Program ============
import shelve import system 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" 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 " return #To create an empty directory there and sync back to shelve dictionary! d = current_dictionary()[args[0]] = {} fs.sync() def pwd(args): d=current_dir print d[-1] def quit(args): sys.exit(0) COMMANDS = {'ls' : ls, 'cd': cd, 'mkdir': mkdir,'pwd':pwd,'quit':quit} 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...')
============ OUTPUT ============
[Root@localhost ~]$ python vfs.py What do you want your username to be? user1> ls Contents of directory /: System Users > cd Users > ls Contents of directory /Users: user1> cd user1> lsContents of directory /Users/user1:> mkdir dir1> cd dir1> pwddir1> quit[Root@localhost ~]$
Write A Python Program For Creating Virtual File System on Linux Environment.
Reviewed by Hardik Pandya
on
3:47:00 PM
Rating:
No comments: