Skip to content

Commit b5571e3

Browse files
committed
new folder based file system created and working
1 parent 5be4edc commit b5571e3

File tree

10 files changed

+128
-106
lines changed

10 files changed

+128
-106
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

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

.github/ISSUE_TEMPLATE/feature_request.md

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

Main.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
1-
from file_system.virtual_fs import * # Import everything from virtual_fs.py
1+
from file_system import create_folder, create_file, list_contents, read_file, delete_file, delete_folder
22

3-
run = True
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)
417

5-
def get_multiline_input(existing_content=""):
6-
"""Function to get multiline input from the user, showing existing content."""
7-
print("Enter the content for the file (Type 'DONE' on a new line to finish):")
8-
9-
# Show existing content if available
10-
if existing_content:
11-
print("\nCurrent content of the file:")
12-
print(existing_content)
13-
print("\nYou can now modify the content. Continue editing...\n")
1418

15-
content = []
16-
while True:
17-
line = input()
18-
if line.strip().upper() == "DONE": # If the user types DONE, stop collecting input
19-
break
20-
content.append(line)
21-
return "\n".join(content) # Join the content with newlines between each line
19+
def main():
20+
"""Command interpreter for the file system."""
21+
print("Welcome to the Python Terminal Virtual File System!")
22+
display_help()
2223

23-
while run:
24-
user_input = input(f"Admin@Python-terminal {current_path} % ")
24+
while True:
25+
command = input("\n>>> ").strip()
26+
if not command:
27+
continue
2528

26-
# Handle empty input case
27-
if user_input.strip() == "": # If the user input is empty or just spaces
28-
continue # Simply skip to the next loop iteration
29+
parts = command.split(" ", 1)
30+
cmd = parts[0].lower()
31+
arg = parts[1] if len(parts) > 1 else None
2932

30-
command = user_input.split(maxsplit=1)
33+
if cmd == "mkdir" and arg:
34+
print(create_folder(arg))
35+
elif cmd == "touch" and arg:
36+
print(create_file(arg))
37+
elif cmd == "ls":
38+
contents = list_contents()
39+
if isinstance(contents, list):
40+
print("\n".join(contents) if contents else "Directory is empty.")
41+
else:
42+
print(contents)
43+
elif cmd == "read" and arg:
44+
print(read_file(arg))
45+
elif cmd == "rm" and arg:
46+
print(delete_file(arg))
47+
elif cmd == "rmdir" and arg:
48+
print(delete_folder(arg))
49+
elif cmd == "help":
50+
display_help()
51+
elif cmd == "exit":
52+
print("Exiting the program. Goodbye!")
53+
break
54+
else:
55+
print("Unknown command. Type 'help' for a list of commands.")
3156

32-
match command:
33-
case ["cd"]:
34-
cd()
35-
case ["cd", path]:
36-
cd(path)
37-
case ["ls"]:
38-
ls()
39-
case ["cat", filename]:
40-
cat(filename)
41-
case ["edit", filename] if len(command) > 1: # Check if the filename is provided
42-
existing_content = cat(filename)
43-
new_content = get_multiline_input(existing_content)
44-
edit(filename, new_content)
45-
print(f"File '{filename}' has been updated.")
46-
case ["quit"]:
47-
run = False
48-
case ["pwd"]:
49-
pwd()
50-
case ["mkdir", dirname]:
51-
mkdir(dirname)
52-
case ["touch", filename]:
53-
touch(filename)
54-
case _:
55-
print(f"zsh: command not found: {user_input}")
57+
if __name__ == "__main__":
58+
main()

__pycache__/Main.cpython-312.pyc

-2.11 KB
Binary file not shown.
4.03 KB
Binary file not shown.

file_system.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
import shutil
3+
4+
# Base directory for the virtual file system
5+
BASE_DIRECTORY = os.path.join(os.getcwd(), "virtual_fs")
6+
7+
# Ensure the base directory exists
8+
if not os.path.exists(BASE_DIRECTORY):
9+
os.makedirs(BASE_DIRECTORY)
10+
11+
12+
def create_folder(folder_name):
13+
"""Creates a folder in the base directory."""
14+
folder_path = os.path.join(BASE_DIRECTORY, folder_name)
15+
try:
16+
os.makedirs(folder_path, exist_ok=True)
17+
return f"Folder '{folder_name}' created."
18+
except Exception as e:
19+
return f"Error creating folder: {e}"
20+
21+
22+
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+
try:
26+
with open(file_path, "w") as file:
27+
file.write(content)
28+
return f"File '{file_name}' created."
29+
except Exception as e:
30+
return f"Error creating file: {e}"
31+
32+
33+
def list_contents():
34+
"""Lists the contents of the base directory."""
35+
try:
36+
return os.listdir(BASE_DIRECTORY)
37+
except Exception as e:
38+
return f"Error listing contents: {e}"
39+
40+
41+
def read_file(file_name):
42+
"""Reads the content of a file."""
43+
file_path = os.path.join(BASE_DIRECTORY, file_name)
44+
if os.path.exists(file_path):
45+
try:
46+
with open(file_path, "r") as file:
47+
return file.read()
48+
except Exception as e:
49+
return f"Error reading file: {e}"
50+
else:
51+
return f"File '{file_name}' does not exist."
52+
53+
54+
def delete_file(file_name):
55+
"""Deletes a file from the base directory."""
56+
file_path = os.path.join(BASE_DIRECTORY, file_name)
57+
if os.path.exists(file_path):
58+
try:
59+
os.remove(file_path)
60+
return f"File '{file_name}' deleted."
61+
except Exception as e:
62+
return f"Error deleting file: {e}"
63+
else:
64+
return f"File '{file_name}' does not exist."
65+
66+
67+
def delete_folder(folder_name):
68+
"""Deletes a folder from the base directory."""
69+
folder_path = os.path.join(BASE_DIRECTORY, folder_name)
70+
if os.path.exists(folder_path):
71+
try:
72+
shutil.rmtree(folder_path)
73+
return f"Folder '{folder_name}' deleted."
74+
except Exception as e:
75+
return f"Error deleting folder: {e}"
76+
else:
77+
return f"Folder '{folder_name}' does not exist."

file_system/__init__.py

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

0 commit comments

Comments
 (0)