-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (78 loc) · 2.5 KB
/
Copy pathmain.py
File metadata and controls
84 lines (78 loc) · 2.5 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
import pathlib as pathlib
def create():
try:
file_name=input("Enter the file name: ")
if pathlib.Path(file_name).exists():
print("File already exists")
return
else:
content=input("Enter the content to be written in the file: ")
with open(file_name,"w") as f:
f.write(content)
except Exception as e:
print(f"An error {e} occurred")
def read():
try:
file_name=input("Enter the file name: ")
if not pathlib.Path(file_name).exists():
print("File does not exist")
return
else:
with open(file_name,"r") as f:
print(f.read())
except Exception as e:
print(f"An error {e} occurred")
def update():
try:
print("Enter 1 to append to the file")
print("Enter 2 to overwrite the file")
print("Enter 3 to change the file name")
c=int(input("Enter your choice: "))
file_name=input("Enter the file name: ")
if not pathlib.Path(file_name).exists():
print("File does not exist")
return
else:
if c==1:
content=input("/n","Enter the content to be appended to the file: ")
with open(file_name,"a") as f:
f.write(content)
elif c==2:
content=input("Enter the content to be written in the file: ")
with open(file_name,"w") as f:
f.write(content)
elif c==3:
new_file_name=input("Enter the new file name: ")
pathlib.Path(file_name).rename(new_file_name)
else:
print("Invalid choice")
except Exception as e:
print(f"An error {e} occurred")
def delete():
try:
file_name=input("Enter the file name: ")
if not pathlib.Path(file_name).exists():
print("File does not exist")
return
else:
pathlib.Path(file_name).unlink()
except Exception as e:
print(f"An error {e} occurred")
try:
print("Enter 1 to create a file")
print("Enter 2 to read a file")
print("Enter 3 to update a file")
print("Enter 4 to delete a file")
choice=int(input("Enter your choice: "))
if choice==1:
create()
elif choice==2:
read()
elif choice==3:
update()
elif choice==4:
delete()
else:
print("Invalid choice")
except Exception as e:
print(f"An error {e} occurred")