-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_exit_save.py
More file actions
34 lines (27 loc) · 897 Bytes
/
no_exit_save.py
File metadata and controls
34 lines (27 loc) · 897 Bytes
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
def overwrite_file():
with open("Exercises/no_save.txt", "w") as file:
file.write("new file line")
def append_file():
with open("Exercises/no_save.txt", "a") as file:
file.write("\nappending line")
def modify_first_line(new_text):
try:
with open("Exercises/no_save.txt", "r") as file:
lines = file.readlines()
if not lines:
print("File is empty")
if lines:
lines[0] = new_text.rstrip("\n") + "\n"
with open("Exercises/no_save.txt", "w") as file:
file.writelines(lines)
except FileNotFoundError:
print("File not found")
def main():
# overwrite_file()
#append_file()
user_input = input()
modify_first_line(user_input)
with open("Exercises/no_save.txt", "r") as file:
print(file.read())
if __name__ == "__main__":
main()