-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonHomework#8.py
147 lines (118 loc) · 4 KB
/
PythonHomework#8.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
fileTxt = ""
fileName = ""
def options():
print("-" * 10)
print("""
what would you like to do?:
1. Read the file
2. Delete the file and start over
3. Append the file
4. Replace single line
5. Exit
""")
menuOpt = int(input("Enter the option number: "))
if menuOpt == 1:
fileName = str(input("Enter filename: "))
#create function to read file
if check_exist(fileName) == True:
readFile(fileName)
input("press ENTER to return to main menu")
options()
else:
print(f"file {fileName} not found. Create file? Y/N ? ")
choice = str(input(":> "))
if choice.upper() == 'Y':
createFile(fileName)
print("returning to main menu... ")
options()
elif choice.upper() == 'N':
print("returning to main menu... ")
options()
else:
print("Invalid option, please re-enter")
#readFile()
#options()
elif menuOpt == 2:
fileName = str(input("Enter file to be deleted: "))
if check_exist(fileName) == True:
deleteFile(fileName)
input("press ENTER to return to main menu")
options()
else:
print(f"!!! file {fileName} not found...")
options()
elif menuOpt == 3:
fileName = str(input("Enter the file to append to: "))
if check_exist(fileName) == True:
appendFile(fileName)
input("press ENTER to return to main menu")
options()
else:
print(f"file {fileName} not found. Create file? Y/N ? ")
choice = str(input(":> "))
if choice.upper() == 'Y':
createFile(fileName)
print("returning to main menu... ")
options()
elif choice.upper() == 'N':
print("returning to main menu... ")
options()
else:
print("Invalid option, please re-enter")
options()
elif menuOpt == 4:
fileName = str(input("Enter the file to replace line in: "))
if check_exist(fileName) == True:
repTxt = str(input("enter text: "))
repLineNbr = int(input("line to replace with text: "))
repLine(fileName, repTxt, repLineNbr)
input("press ENTER to return to main menu")
options()
else:
print(f"file {fileName} not found. Create file? Y/N ? ")
choice = str(input(":> "))
if choice.upper() == 'Y':
createFile(fileName)
print("returning to main menu... ")
options()
elif choice.upper() == 'N':
print("returning to main menu... ")
options()
else:
print("Invalid option, please re-enter")
options()
elif menuOpt == 5:
exit()
def repLine(fileName, repTxt, repLineNbr):
with open(fileName,"r") as txt:
txtLines = txt.readlines()
for ln in txtLines:
if txtLines.index(ln) == repLineNbr -1:
txtLines[txtLines.index(ln)] = repTxt + "\n"
break
with open(fileName,"w") as txt:
for ln in txtLines:
txt.write(ln)
def appendFile(fileName):
with open(fileName,"a") as txt:
appTxt = str(input("append text: "))
txt.writelines(appTxt + "\n")
options()
def deleteFile(fileName):
os.remove(fileName)
print(f"file {fileName} has been removed")
def createFile(fileName):
with open(fileName,"w") as txt:
print(f"file {fileName} has been created")
def readFile(fileName):
with open(fileName,"r") as fileTxt:
txt = fileTxt.read()
print(txt)
def check_exist(fileName):
if os.path.exists(fileName) == True:
print(f"file {fileName} exists in {os.path.abspath(fileName)}")
return True
else:
return False
options()