-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandling_project.py
More file actions
103 lines (84 loc) · 2.81 KB
/
FileHandling_project.py
File metadata and controls
103 lines (84 loc) · 2.81 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
from pathlib import Path
import os
def readfileandfolder():
path = Path('')
items = list(path.rglob('*'))
for i, items in enumerate(items):
print(f"{i+1} : {items}")
def createfile():
try:
readfileandfolder()
name = input("please tell your file name:- ")
p = Path(name)
if not p.exists():
with open(p, 'w') as fs:
data = input("what you want to write in this file:- ")
fs.write(data)
print(f"FILE CREATED SUCCESSFULLY")
else:
print("this file already exits")
except Exception as err:
print(f"An error occured as {err}")
def readfile():
try:
readfileandfolder()
name = input("which file you want to read:-")
p = Path(name)
if p.exists() and p.is_file():
with open(p, 'r') as fs:
data = fs.read()
print(data)
print("Readed successfully")
else:
print("the file doesnot exits")
except Exception as err:
print(f"An error occured as {err}")
def updatefile():
try:
readfileandfolder()
name = input("tell which file you want to update:-")
p = Path(name)
if p.exists() and p.is_file():
print("press 1 for change the name of your file:- ")
print("press 2 for overwriting the data of your file")
print("press 3 for appending some content in your file")
res = int(input("tell your response:-"))
if res == 1:
name2 = input("tell your new file name:-")
p2 = Path(name2)
p.rename(p2)
if res == 2:
with open(p, 'w') as fs:
data = input("tell what you want to write this is overwrite the data:-")
fs.write(data)
if res == 3:
with open(p, 'a') as fs:
data = input("tell what you want to append:-")
fs.write(" " + data)
except Exception as err:
print(f"An error occured as {err}")
def deletefile():
try:
readfileandfolder()
name = input("which file you want to delete:-")
p = Path(name)
if p.exists() and p.is_file():
os.remove(name)
print("file remove successfully")
else:
print("no such file exits")
except Exception as err:
print(f"An error occured as {err}")
print("press 1 for creating a file")
print("press 2 for reading a file")
print("press 3 for updating a file")
print("press 4 for deletion a file")
check = int(input("please tell your response:-"))
if check == 1:
createfile()
if check == 2:
readfile()
if check == 3:
updatefile()
if check == 4:
deletefile()