Skip to content

Commit 3537547

Browse files
committed
version 2.0.1
works from a dictionary
1 parent cb462d9 commit 3537547

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

Main.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ def get_multiline_input(existing_content=""):
3939
case ["cat", filename]:
4040
cat(filename)
4141
case ["edit", filename] if len(command) > 1: # Check if the filename is provided
42-
# Read the current content of the file
4342
existing_content = cat(filename)
44-
45-
# Get the new content for the file by prompting the user
4643
new_content = get_multiline_input(existing_content)
47-
48-
# Update the file's content with the new content
4944
edit(filename, new_content)
5045
print(f"File '{filename}' has been updated.")
5146
case ["quit"]:
5247
run = False
53-
case ["tree"]:
54-
tree()
48+
case ["pwd"]:
49+
pwd()
50+
case ["mkdir", dirname]:
51+
mkdir(dirname)
52+
case ["touch", filename]:
53+
touch(filename)
5554
case _:
5655
print(f"zsh: command not found: {user_input}")

__pycache__/Main.cpython-312.pyc

2.11 KB
Binary file not shown.
292 Bytes
Binary file not shown.

file_system/virtual_fs.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Virtual filesystem definition
22
virtual_fs = {
3-
"/": {"home": {}, "var": {}, "tmp": {}},
3+
"/": {"bin": {}, "boot": {}, "dev": {}, "etc": {}, "home": {}, "lib": {}, "media": {}, "mnt": {}, "opt": {}, "sbin": {}, "srv": {}, "tmp": {}, "usr": {}, "proc": {}},
44
"/home": {"user": {}},
55
"/home/user": {"file1.txt": "HelloWorld!", "documents": {}},
66
"/var": {},
@@ -13,7 +13,7 @@ def cd(path=None):
1313
"""Change the current directory in the virtual filesystem."""
1414
global current_path
1515
if not path or path.strip() == "":
16-
print(current_path)
16+
return
1717
elif path == "..":
1818
if current_path != "/":
1919
current_path = "/".join(current_path.rstrip("/").split("/")[:-1])
@@ -25,7 +25,6 @@ def cd(path=None):
2525
else:
2626
print(f"cd: no such file or directory: {path}")
2727

28-
2928
def ls():
3029
"""List the contents of the current directory."""
3130
contents = virtual_fs.get(current_path, {})
@@ -60,22 +59,35 @@ def edit(filename, new_content):
6059
else:
6160
print(f"edit: {filename}: No such file or directory")
6261

62+
def pwd():
63+
print(current_path)
6364

64-
def print_tree(path, indent=""):
65-
"""Recursively print the directory tree, listing files and subdirectories."""
66-
# Get contents of the current path
67-
contents = virtual_fs.get(path, {})
68-
69-
# Iterate through the contents of the directory
70-
for item, content in contents.items():
71-
# If the content is a dictionary, it's a directory, so print its name and recurse into it
72-
if isinstance(content, dict):
73-
print(f"{indent}{item}/") # Print directory with a slash at the end
74-
print_tree(f"{path}/{item}", indent + " ") # Recurse into the directory
75-
else:
76-
# If it's not a dictionary, it's a file, so print its name
77-
print(f"{indent}{item}") # Print file name
65+
def mkdir(directory_name):
66+
"""Create a new directory in the current directory."""
67+
global current_path
68+
69+
# Get the current directory's contents
70+
current_dir_contents = virtual_fs.get(current_path, {})
71+
72+
# Check if the directory already exists
73+
if directory_name in current_dir_contents:
74+
print(f"mkdir: cannot create directory '{directory_name}': File exists")
75+
else:
76+
# Create the new directory as an empty dictionary
77+
current_dir_contents[directory_name] = {}
78+
print(f"Directory '{directory_name}' created.")
7879

79-
def tree():
80-
"""Print the tree of the current directory starting from the root."""
81-
print_tree(current_path)
80+
def touch(filename):
81+
"""Create a new empty text file in the current directory."""
82+
global current_path
83+
84+
# Get the current directory's contents
85+
current_dir_contents = virtual_fs.get(current_path, {})
86+
87+
# Check if the file already exists
88+
if filename in current_dir_contents:
89+
print(f"touch: cannot create file '{filename}': File exists")
90+
else:
91+
# Create the new file with empty content (represented as an empty string)
92+
current_dir_contents[filename] = ""
93+
print(f"File '{filename}' created.")

0 commit comments

Comments
 (0)