11import os
2- import shutil
32
43# Base directory for the virtual file system
54BASE_DIRECTORY = os .path .join (os .getcwd (), "virtual_fs" )
87if not os .path .exists (BASE_DIRECTORY ):
98 os .makedirs (BASE_DIRECTORY )
109
10+ # Current working directory starts at BASE_DIRECTORY
11+ current_directory = BASE_DIRECTORY
12+
1113
1214def create_folder (folder_name ):
13- """Creates a folder in the base directory."""
14- folder_path = os .path .join (BASE_DIRECTORY , folder_name )
15+ """Creates a folder in the current directory."""
16+ folder_path = os .path .join (current_directory , folder_name )
1517 try :
1618 os .makedirs (folder_path , exist_ok = True )
1719 return f"Folder '{ folder_name } ' created."
@@ -20,8 +22,8 @@ def create_folder(folder_name):
2022
2123
2224def create_file (file_name , content = "" ):
23- """Creates a file in the base directory with optional content."""
24- file_path = os .path .join (BASE_DIRECTORY , file_name )
25+ """Creates a file in the current directory with optional content."""
26+ file_path = os .path .join (current_directory , file_name )
2527 try :
2628 with open (file_path , "w" ) as file :
2729 file .write (content )
@@ -31,16 +33,16 @@ def create_file(file_name, content=""):
3133
3234
3335def list_contents ():
34- """Lists the contents of the base directory."""
36+ """Lists the contents of the current directory."""
3537 try :
36- return os .listdir (BASE_DIRECTORY )
38+ return os .listdir (current_directory )
3739 except Exception as e :
3840 return f"Error listing contents: { e } "
3941
4042
4143def read_file (file_name ):
4244 """Reads the content of a file."""
43- file_path = os .path .join (BASE_DIRECTORY , file_name )
45+ file_path = os .path .join (current_directory , file_name )
4446 if os .path .exists (file_path ):
4547 try :
4648 with open (file_path , "r" ) as file :
@@ -52,8 +54,8 @@ def read_file(file_name):
5254
5355
5456def delete_file (file_name ):
55- """Deletes a file from the base directory."""
56- file_path = os .path .join (BASE_DIRECTORY , file_name )
57+ """Deletes a file from the current directory."""
58+ file_path = os .path .join (current_directory , file_name )
5759 if os .path .exists (file_path ):
5860 try :
5961 os .remove (file_path )
@@ -65,13 +67,39 @@ def delete_file(file_name):
6567
6668
6769def delete_folder (folder_name ):
68- """Deletes a folder from the base directory."""
69- folder_path = os .path .join (BASE_DIRECTORY , folder_name )
70+ """Deletes a folder from the current directory."""
71+ folder_path = os .path .join (current_directory , folder_name )
7072 if os .path .exists (folder_path ):
7173 try :
72- shutil . rmtree (folder_path )
74+ os . rmdir (folder_path )
7375 return f"Folder '{ folder_name } ' deleted."
7476 except Exception as e :
7577 return f"Error deleting folder: { e } "
7678 else :
7779 return f"Folder '{ folder_name } ' does not exist."
80+
81+
82+ def change_directory (new_directory ):
83+ """Changes the current directory."""
84+ global current_directory
85+ target_directory = os .path .join (current_directory , new_directory )
86+ if os .path .exists (target_directory ) and os .path .isdir (target_directory ):
87+ current_directory = target_directory
88+ return f"Changed directory to '{ print_working_directory ()} '."
89+ else :
90+ return f"Directory '{ new_directory } ' does not exist."
91+
92+
93+ def go_back ():
94+ """Navigates to the parent directory."""
95+ global current_directory
96+ if current_directory != BASE_DIRECTORY :
97+ current_directory = os .path .dirname (current_directory )
98+ return f"Moved back to '{ print_working_directory ()} '."
99+ else :
100+ return "You are already at the base directory."
101+
102+
103+ def print_working_directory ():
104+ """Returns the current working directory, relative to BASE_DIRECTORY."""
105+ return "/" + os .path .relpath (current_directory , BASE_DIRECTORY ).replace ("\\ " , "/" )
0 commit comments