virtualfs.py
1: import shelve
2: import os,sys
3: fs=shelve.open('myfilesystem.fs', writeback=True)
4: current_dir=[]
5: def install(fs):
6: uname=raw_input("Enter user name : ")
7: fs[""]={"System":{},"Users":{uname:{}}}
8: def current_dictionary():
9: """Displays dictionary containing the files in the current directory"""
10: d=fs[""]
11: for key in current_dir:
12: d=d[key]
13: return d
14: def ls(args):
15: print 'Contents of directory are : '"/"+"/".join(current_dir)+':'
16: for i in current_dictionary():
17: print i
18: def cd(args):
19: if len(args)!=1:
20: print "Usage: cd <directory>"
21: return
22: if args[0]=="..":
23: if len(current_dir)==0:
24: print "Sorry!!! Cannot go above root"
25: else:
26: current_dir.pop()
27: elif args[0] not in current_dictionary():
28: print "Directory "+ args[0]+" not found"
29: else:
30: current_dir.append(args[0])
31: def mkdir(args):
32: if len(args)!=1:
33: print "Usage: mkdir <directory>"
34: return
35: d=current_dictionary()[args[0]]={}
36: print "Directory created..."
37: fs.sync()
38: def rmdir(args):
39: if len(args)!=1:
40: print "Usage: rmdir <directory>"
41: return
42: elif args[0] not in current_dictionary():
43: print "Directory "+ args[0]+" not found"
44: else:
45: d=current_dictionary()
46: print d
47: for key1 in d:
48: if (key1==args[0]):
49: del d[key1]
50: print "Directory removed..."
51: fs.sync()
52: break
53: COMMANDS = {'ls' : ls,'cd' : cd, 'mkdir' : mkdir, 'rmdir' : rmdir}
54: install(fs)
55: while True:
56: raw=raw_input('>')
57: cmd=raw.split()[0]
58: if cmd in COMMANDS:
59: COMMANDS[cmd](raw.split()[1:])
60: raw_input('Press the enter key to close the filesystem.........')
Please update it for creating a file
ReplyDelete