Skip to content

Commit f152d96

Browse files
committed
v2.2.0 full relese
1 parent b5571e3 commit f152d96

File tree

4 files changed

+55
-108
lines changed

4 files changed

+55
-108
lines changed

Main.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
from file_system import create_folder, create_file, list_contents, read_file, delete_file, delete_folder
1+
from file_system import (
2+
create_folder, create_file, list_contents, read_file, delete_file,
3+
delete_folder, change_directory, go_back, print_working_directory
4+
)
25

36
def display_help():
47
"""Displays available commands."""
58
commands = """
69
Available commands:
710
- mkdir <folder_name> : Create a folder
811
- touch <file_name> : Create a file
9-
- ls : List contents of the virtual directory
12+
- ls : List contents of the current directory
1013
- read <file_name> : Read a file
1114
- rm <file_name> : Delete a file
1215
- rmdir <folder_name> : Delete a folder
16+
- cd <folder_name> : Change directory
17+
- back : Move to the parent directory
18+
- pwd : Print the current working directory
1319
- help : Show this help menu
1420
- exit : Exit the program
1521
"""
@@ -46,6 +52,12 @@ def main():
4652
print(delete_file(arg))
4753
elif cmd == "rmdir" and arg:
4854
print(delete_folder(arg))
55+
elif cmd == "cd" and arg:
56+
print(change_directory(arg))
57+
elif cmd == "back":
58+
print(go_back())
59+
elif cmd == "pwd":
60+
print(print_working_directory())
4961
elif cmd == "help":
5062
display_help()
5163
elif cmd == "exit":
0 Bytes
Binary file not shown.

file_system.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import shutil
32

43
# Base directory for the virtual file system
54
BASE_DIRECTORY = os.path.join(os.getcwd(), "virtual_fs")
@@ -8,10 +7,13 @@
87
if 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

1214
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)
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

2224
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)
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

3335
def 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

4143
def 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

5456
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)
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

6769
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)
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("\\", "/")

virtual_fs.py

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)