forked from mosh-hamedani/python-projects-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_text_editor.py
More file actions
39 lines (30 loc) · 824 Bytes
/
simple_text_editor.py
File metadata and controls
39 lines (30 loc) · 824 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
35
36
37
38
39
import os
def read_file(filename):
with open(filename, 'r') as file:
return file.read()
def write_file(filename, content):
with open(filename, 'w') as file:
file.write(content)
def get_user_input():
print('\nEnter your text (type SAVE on a new line to save and exist):')
lines = []
while True:
line = input()
if line == 'SAVE':
break
lines.append(line)
return '\n'.join(lines)
def main():
filename = input('Enter the filename to open or create: ').strip()
try:
if os.path.exists(filename):
print(read_file(filename))
else:
write_file(filename, '')
content = get_user_input()
write_file(filename, content)
print(f'{filename} saved.')
except OSError:
print(f'{filename} could not be opened.')
if __name__ == '__main__':
main()