-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_text_editor.py
More file actions
29 lines (25 loc) · 1.13 KB
/
Copy pathsimple_text_editor.py
File metadata and controls
29 lines (25 loc) · 1.13 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
# program to allow users to open an existing file or create a new one, edit content and hen save the changes typing the save command
def write_content(file_name):
with open(file_name, 'w') as f:
f.write(input("Enter your text (type 'SAVE' on a new line to save and exit): "))
def display_or_write(choice,file_name):
if choice == '1':
with open(file_name, 'r') as f:
file_content = f.read()
print(file_content)
elif choice == '2':
write_content(file_name)
while True:
file_name = input("Enter the filename to open or create (.txt format): ")
# print(type(file_name))
try:
with open(file_name, 'r') as f:
choice = input("file found! Enter 1 to display file content, Enter 2 to write to file: ")
display_or_write(choice, file_name)
except FileNotFoundError:
user_choice = input(f"{file_name} not found! creating a new file\nWould you like to create a new file {file_name}? (y/n): ").lower()
if user_choice =='y':
write_content(file_name)
else:
print("program terminated!")
break