-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp1.py
More file actions
90 lines (75 loc) · 2.53 KB
/
app1.py
File metadata and controls
90 lines (75 loc) · 2.53 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
import os
def create_file(filename):
try:
with open(filename, 'x') as f:
print(f"File name {filename}: Created successfully!")
except FileExistsError:
print(f'File name {filename} already exists!')
except Exception as e:
print('An error occurred!')
def view_all_file():
files = os.listdir()
if not files:
print('No file found!')
else:
print('Files in directory:')
for file in files:
print(file)
def delete_file(filename):
try:
os.remove(filename)
print(f'{filename} has been deleted successfully!')
except FileNotFoundError:
print('File not found!')
except Exception as e:
print('An error occurred!')
def read_file(filename):
try:
with open(filename, 'r') as f:
content = f.read()
print(f"Content of '{filename}':\n{content}")
except FileNotFoundError:
print(f"{filename} doesn't exist!")
except Exception as e:
print('An error occurred!')
def edit_file(filename):
try:
with open(filename,'a') as f:
content = input("Enter data to add = ")
f.write(content + "\n")
print(f'Content added to {filename} Successfully!')
except FileNotFoundError:
print(f"{filename} doesn't exist!")
except Exception as e:
print('An error occurred!')
def main():
while True:
print("\nFILE MANAGEMENT APP")
print('1: Create file')
print('2: View all files')
print('3: Delete file')
print('4: Read file')
print('5: Edit file')
print('6: Exit')
choice = input('Enter your choice (1-6) = ')
if choice == '1':
filename = input("Enter file name to create = ")
create_file(filename)
elif choice == '2':
view_all_file()
elif choice == '3':
filename = input("Enter file name you want to delete = ")
delete_file(filename)
elif choice =='4':
filename = input("Enter file name to read = ")
read_file(filename)
elif choice == '5':
filename = input("Enter file name to edit = ")
edit_file(filename)
elif choice == '6':
print('Closing the app....')
break
else:
print('Invalid choice!')
if __name__ == "__main__":
main()