-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
84 lines (76 loc) · 2.69 KB
/
file.py
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
# Define a dictionary to store the file structure
file_structure = {
'root': {
'type': 'directory',
'content': {}
}
}
# Define the current working directory
current_directory = file_structure['root']
# Define a function to list the contents of the current directory
def list_directory():
print('Contents of', current_directory['type'], ':')
for item in current_directory['content']:
print(item)
# Define a function to change the current directory
def change_directory(directory):
global current_directory
if directory in current_directory['content']:
current_directory = current_directory['content'][directory]
else:
print('Directory not found')
# Define a function to create a new directory
def make_directory(directory):
if directory not in current_directory['content']:
current_directory['content'][directory] = {
'type': 'directory',
'content': {}
}
else:
print('Directory already exists')
# Define a function to delete a directory or file
def delete(item):
if item in current_directory['content']:
del current_directory['content'][item]
else:
print('Item not found')
# Define a function to move a directory or file
def move(item, destination_directory):
if item in current_directory['content']:
if destination_directory in current_directory['content']:
destination = current_directory['content'][destination_directory]
if 'type' in destination and destination['type'] == 'directory':
destination['content'][item] = current_directory['content'][item]
del current_directory['content'][item]
else:
print('Destination is not a directory')
else:
print('Destination directory not found')
else:
print('Item not found')
# Define a function to print the current working directory
def present_working_directory():
print('Current directory:', current_directory)
# Main loop to handle user input
while True:
user_input = input('Enter a command: ')
commands = user_input.split()
if len(commands) == 0:
continue
if commands[0] == 'ls':
list_directory()
elif commands[0] == 'cd':
if len(commands) == 1:
change_directory('root')
else:
change_directory(commands[1])
elif commands[0] == 'mkdir':
if len(commands) == 1:
print('Please specify a directory name')
else:
make_directory(commands[1])
elif commands[0] == 'delete':
if len(commands) == 1:
print('Please specify an item to delete')
else:
delete(commands[1])