-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.py
More file actions
116 lines (103 loc) · 3.87 KB
/
Copy pathfilesystem.py
File metadata and controls
116 lines (103 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sys
# File explorer's default location #
current_dir = "root/"
# To hold "files" and "directories" as dictionaries in a list #
fileList = []
# For creating "files" and "directories" when the touch and mkdir commands are used #
class Link:
def __init__(self, name, parent_directory, is_directory=False):
self.name = name
self.parent_directory = parent_directory
self.is_directory = is_directory
# Receive and validate user input, call relevant function #
def command_prompt():
global command
command = input(
f"Current Directory: {current_dir}\nPlease choose a command: ls, mkdir, cd, touch, or exit\n$ ")
# Check that mkdir, touch, and cd have arguments #
if "mkdir" in command.lower() and len(command.split(" ")) <= 1:
print("Error: the mkdir command requires an argument. Try mkdir <dir_name>.")
command_prompt()
elif "touch" in command.lower() and len(command.split(" ")) <= 1:
print("Error: the touch command requires an argument. Try touch <file_name>.")
command_prompt()
elif "cd" in command.lower() and len(command.split(" ")) <= 1:
print("Error: the cd command requires an argument. Try cd <dir_name>.")
command_prompt()
# Calling functions according to user input #
elif "ls" in command.lower():
ls()
elif "mkdir" in command.lower():
mkdir()
elif "cd" in command.lower():
cd()
elif "touch" in command.lower():
touch()
elif "exit" in command.lower():
sys.exit("Goodbye!")
else:
print("Invalid command.")
command_prompt()
# FUNCTIONS #
def ls():
# Checks that directory has contents and prints them #
print("Directory contents:")
n = 0
for x in fileList:
if x["parent_directory"] == current_dir:
n += 1
print(x["name"])
if n < 1:
print("This directory is empty.")
command_prompt()
def mkdir():
# Checks for potential duplicates and creates directories #
global command
new_dir = command.lower().split()[1]
for x in fileList:
if new_dir + "/" == x["name"] and current_dir == x["parent_directory"]:
print(
f"A directory called '{new_dir}' already exists in this location.")
command_prompt()
fileList.append(Link(new_dir + "/", current_dir, True).__dict__)
print(f"Directory '{new_dir}' has been created.")
command_prompt()
def cd():
global current_dir
dir = command.lower().split()[1]
# Navigating "up" a level. Goes no further than "root/" #
if dir == "..":
if current_dir == "root/":
print("You are in the root directory. You may go no deeper.")
else:
# Split directory path by "/", delete last directory, rejoin directory path with "/" #
current_dir = current_dir.split("/")
del current_dir[-2]
current_dir = "/".join(current_dir)
command_prompt()
elif dir:
n = 0
for x in fileList:
# Check that subdirectory exists and is in current directory #
if dir in x["name"] and x["is_directory"] and current_dir == x["parent_directory"]:
n += 1
if n:
current_dir = current_dir + dir + "/"
# Otherwise, throw error #
else:
print("Directory not found.")
command_prompt()
def touch():
# Checks for potential duplicates and creates files #
global command
new_file = command.lower().split()[1]
for x in fileList:
if new_file == x["name"] and current_dir == x["parent_directory"]:
print(
f"A file named '{new_file}' already exists in this location.")
command_prompt()
fileList.append(Link(new_file, current_dir).__dict__)
print(f"File '{new_file}' has been created.")
command_prompt()
if __name__ == '__main__':
command_prompt()