1
1
import os
2
- import shutil
3
2
4
3
# Base directory for the virtual file system
5
4
BASE_DIRECTORY = os .path .join (os .getcwd (), "virtual_fs" )
8
7
if not os .path .exists (BASE_DIRECTORY ):
9
8
os .makedirs (BASE_DIRECTORY )
10
9
10
+ # Current working directory starts at BASE_DIRECTORY
11
+ current_directory = BASE_DIRECTORY
12
+
11
13
12
14
def 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 )
15
17
try :
16
18
os .makedirs (folder_path , exist_ok = True )
17
19
return f"Folder '{ folder_name } ' created."
@@ -20,8 +22,8 @@ def create_folder(folder_name):
20
22
21
23
22
24
def 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 )
25
27
try :
26
28
with open (file_path , "w" ) as file :
27
29
file .write (content )
@@ -31,16 +33,16 @@ def create_file(file_name, content=""):
31
33
32
34
33
35
def list_contents ():
34
- """Lists the contents of the base directory."""
36
+ """Lists the contents of the current directory."""
35
37
try :
36
- return os .listdir (BASE_DIRECTORY )
38
+ return os .listdir (current_directory )
37
39
except Exception as e :
38
40
return f"Error listing contents: { e } "
39
41
40
42
41
43
def read_file (file_name ):
42
44
"""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 )
44
46
if os .path .exists (file_path ):
45
47
try :
46
48
with open (file_path , "r" ) as file :
@@ -52,8 +54,8 @@ def read_file(file_name):
52
54
53
55
54
56
def 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 )
57
59
if os .path .exists (file_path ):
58
60
try :
59
61
os .remove (file_path )
@@ -65,13 +67,39 @@ def delete_file(file_name):
65
67
66
68
67
69
def 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 )
70
72
if os .path .exists (folder_path ):
71
73
try :
72
- shutil . rmtree (folder_path )
74
+ os . rmdir (folder_path )
73
75
return f"Folder '{ folder_name } ' deleted."
74
76
except Exception as e :
75
77
return f"Error deleting folder: { e } "
76
78
else :
77
79
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