Skip to content

Commit 9ab921c

Browse files
committed
file_system
1 parent 035c3fa commit 9ab921c

File tree

9 files changed

+133
-41
lines changed

9 files changed

+133
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Main.py

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
from file_system import create_folder, create_file, list_contents, read_file, delete_file, delete_folder
2-
3-
def display_help():
4-
"""Displays available commands."""
5-
commands = """
6-
Available commands:
7-
- mkdir <folder_name> : Create a folder
8-
- touch <file_name> : Create a file
9-
- ls : List contents of the virtual directory
10-
- read <file_name> : Read a file
11-
- rm <file_name> : Delete a file
12-
- rmdir <folder_name> : Delete a folder
13-
- help : Show this help menu
14-
- exit : Exit the program
15-
"""
16-
print(commands)
1+
<<<<<<< Updated upstream
2+
from file_system.virtual_fs import * # Import everything from virtual_fs.py
173

4+
run = True
5+
=======
6+
from file_system import *
187

19-
def main():
20-
"""Command interpreter for the file system."""
21-
print("Welcome to the Python Terminal Virtual File System!")
22-
display_help()
238

9+
>>>>>>> Stashed changes
10+
11+
def get_multiline_input(existing_content=""):
12+
"""Function to get multiline input from the user, showing existing content."""
13+
print("Enter the content for the file (Type 'DONE' on a new line to finish):")
14+
15+
# Show existing content if available
16+
if existing_content:
17+
print("\nCurrent content of the file:")
18+
print(existing_content)
19+
print("\nYou can now modify the content. Continue editing...\n")
20+
21+
<<<<<<< Updated upstream
22+
content = []
2423
while True:
25-
command = input("\n>>> ").strip()
24+
line = input()
25+
if line.strip().upper() == "DONE": # If the user types DONE, stop collecting input
26+
break
27+
content.append(line)
28+
return "\n".join(content) # Join the content with newlines between each line
29+
=======
30+
def main():
31+
while True:
32+
command = input("Admin@Python-Terminal ~ % ").strip()
2633
if not command:
2734
continue
2835

@@ -40,19 +47,57 @@ def main():
4047
print("\n".join(contents) if contents else "Directory is empty.")
4148
else:
4249
print(contents)
43-
elif cmd == "read" and arg:
44-
print(read_file(arg))
50+
elif cmd == "cat" and arg:
51+
print(cat(arg))
4552
elif cmd == "rm" and arg:
4653
print(delete_file(arg))
4754
elif cmd == "rmdir" and arg:
4855
print(delete_folder(arg))
56+
elif cmd == "cd" and arg:
57+
print(change_directory(arg))
58+
elif cmd == "cd ..":
59+
print(go_back())
60+
elif cmd == "pwd":
61+
print(print_working_directory())
4962
elif cmd == "help":
5063
display_help()
51-
elif cmd == "exit":
64+
elif cmd == "quit":
5265
print("Exiting the program. Goodbye!")
5366
break
5467
else:
55-
print("Unknown command. Type 'help' for a list of commands.")
68+
print(f"zsh: command not found: {command}")
69+
>>>>>>> Stashed changes
70+
71+
while run:
72+
user_input = input(f"Admin@Python-terminal {current_path} % ")
73+
74+
# Handle empty input case
75+
if user_input.strip() == "": # If the user input is empty or just spaces
76+
continue # Simply skip to the next loop iteration
77+
78+
command = user_input.split(maxsplit=1)
5679

57-
if __name__ == "__main__":
58-
main()
80+
match command:
81+
case ["cd"]:
82+
cd()
83+
case ["cd", path]:
84+
cd(path)
85+
case ["ls"]:
86+
ls()
87+
case ["cat", filename]:
88+
cat(filename)
89+
case ["edit", filename] if len(command) > 1: # Check if the filename is provided
90+
existing_content = cat(filename)
91+
new_content = get_multiline_input(existing_content)
92+
edit(filename, new_content)
93+
print(f"File '{filename}' has been updated.")
94+
case ["quit"]:
95+
run = False
96+
case ["pwd"]:
97+
pwd()
98+
case ["mkdir", dirname]:
99+
mkdir(dirname)
100+
case ["touch", filename]:
101+
touch(filename)
102+
case _:
103+
print(f"zsh: command not found: {user_input}")

__pycache__/Main.cpython-312.pyc

2.11 KB
Binary file not shown.
2.12 KB
Binary file not shown.

file_system.py

Lines changed: 60 additions & 14 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

41-
def read_file(file_name):
43+
def cat(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,57 @@ 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("\\", "/")
106+
107+
def display_help():
108+
"""Displays available commands."""
109+
commands = """
110+
Available commands:
111+
- mkdir <folder_name> : Create a folder
112+
- touch <file_name> : Create a file
113+
- ls : List contents of the current directory
114+
- cat <file_name> : Read a file
115+
- rm <file_name> : Delete a file
116+
- rmdir <folder_name> : Delete a folder
117+
- cd <folder_name> : Change directory
118+
- cd .. : Move to the parent directory
119+
- pwd : Print the current working directory
120+
- help : Show this help menu
121+
- quit : Exit the program
122+
"""
123+
print(commands)

file_system/__init__.py

Whitespace-only changes.
194 Bytes
Binary file not shown.
3.68 KB
Binary file not shown.
File renamed without changes.

0 commit comments

Comments
 (0)