11# Virtual filesystem definition
22virtual_fs = {
3- "/" : {"home" : {}, "var " : {}, "tmp" : {}},
3+ "/" : {"bin" : {}, "boot" : {}, "dev" : {}, "etc" : {}, " home" : {}, "lib " : {}, "media" : {}, "mnt" : {}, "opt" : {}, "sbin" : {}, "srv" : {}, " tmp" : {}, "usr" : {}, "proc " : {}},
44 "/home" : {"user" : {}},
55 "/home/user" : {"file1.txt" : "HelloWorld!" , "documents" : {}},
66 "/var" : {},
@@ -13,7 +13,7 @@ def cd(path=None):
1313 """Change the current directory in the virtual filesystem."""
1414 global current_path
1515 if not path or path .strip () == "" :
16- print ( current_path )
16+ return
1717 elif path == ".." :
1818 if current_path != "/" :
1919 current_path = "/" .join (current_path .rstrip ("/" ).split ("/" )[:- 1 ])
@@ -25,7 +25,6 @@ def cd(path=None):
2525 else :
2626 print (f"cd: no such file or directory: { path } " )
2727
28-
2928def ls ():
3029 """List the contents of the current directory."""
3130 contents = virtual_fs .get (current_path , {})
@@ -60,22 +59,35 @@ def edit(filename, new_content):
6059 else :
6160 print (f"edit: { filename } : No such file or directory" )
6261
62+ def pwd ():
63+ print (current_path )
6364
64- def print_tree ( path , indent = "" ):
65- """Recursively print the directory tree, listing files and subdirectories ."""
66- # Get contents of the current path
67- contents = virtual_fs . get ( path , {})
68-
69- # Iterate through the contents of the directory
70- for item , content in contents . items ():
71- # If the content is a dictionary, it's a directory, so print its name and recurse into it
72- if isinstance ( content , dict ) :
73- print (f"{ indent } { item } /" ) # Print directory with a slash at the end
74- print_tree ( f" { path } / { item } " , indent + " " ) # Recurse into the directory
75- else :
76- # If it's not a dictionary, it's a file, so print its name
77- print (f"{ indent } { item } " ) # Print file name
65+ def mkdir ( directory_name ):
66+ """Create a new directory in the current directory ."""
67+ global current_path
68+
69+ # Get the current directory's contents
70+ current_dir_contents = virtual_fs . get ( current_path , {})
71+
72+ # Check if the directory already exists
73+ if directory_name in current_dir_contents :
74+ print (f"mkdir: cannot create directory ' { directory_name } ': File exists" )
75+ else :
76+ # Create the new directory as an empty dictionary
77+ current_dir_contents [ directory_name ] = {}
78+ print (f"Directory ' { directory_name } ' created." )
7879
79- def tree ():
80- """Print the tree of the current directory starting from the root."""
81- print_tree (current_path )
80+ def touch (filename ):
81+ """Create a new empty text file in the current directory."""
82+ global current_path
83+
84+ # Get the current directory's contents
85+ current_dir_contents = virtual_fs .get (current_path , {})
86+
87+ # Check if the file already exists
88+ if filename in current_dir_contents :
89+ print (f"touch: cannot create file '{ filename } ': File exists" )
90+ else :
91+ # Create the new file with empty content (represented as an empty string)
92+ current_dir_contents [filename ] = ""
93+ print (f"File '{ filename } ' created." )
0 commit comments