-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextFile.py
More file actions
executable file
·134 lines (121 loc) · 3.89 KB
/
Copy pathtextFile.py
File metadata and controls
executable file
·134 lines (121 loc) · 3.89 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
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
#!/usr/bin/env python3
'textFile.py -- create/display/edit text files'
import sys,os
from os import linesep as ls
from shutil import get_terminal_size as term_size
# calculate number of # to fill term width
nb_hash = term_size()[0]
hash_line = "#" * nb_hash
prg_dialogue = "Enter file name ('back' to return to main screen, 'exit' to exit program): "
# function to exit program
def exit_prog():
print("Program exit!")
sys.exit(0)
# function to create file
def write_file():
while True:
fname = input(prg_dialogue).strip()
create_file = True
if fname == "back":
print("Going back to main program")
create_file = False
break
elif fname == "exit":
exit_prog()
elif os.path.exists(fname):
print("ERROR: %s already exists" % fname)
else:
break
if create_file:
# get file content (text) lines
all = []
print("%sEnter lines ('.' by itself to quit).%s" % (ls, ls))
# loop until user terminates input
while True:
entry = input("> ")
if entry == '.':
break
else:
all.append(entry)
# write lines to file with proper line-ending
with open(fname, "w") as fobj:
fobj.write(ls.join(all))
fobj.write(ls)
print("DONE!")
# function to display file
def read_file():
#get filename
while True:
fname = input(prg_dialogue).strip()
if fname == "back":
print("Going back to main program")
break
elif fname == "exit":
exit_prog()
elif not os.path.exists(fname):
print("ERROR: %s doesn't exist" % fname)
continue
else:
#display contents to the screen
with open(fname, "r") as fobj:
print(hash_line)
for each_line in fobj:
print(each_line.strip(ls))
print(hash_line)
break
# function to edit file
def edit_file():
#get filename
while True:
fname = input(prg_dialogue).strip()
if fname == "back":
print("Going back to main program")
break
elif fname == "exit":
exit_prog()
elif not os.path.exists(fname):
print("ERROR: %s doesn't exist" % fname)
continue
else:
#create list to store file contents
file_list = []
with open(fname, "r") as fobj:
for each_line in fobj:
file_list.append(each_line)
new_file_list = []
print("Enter edited line ('.' to leave intact)")
for item in file_list:
print(item.strip("\n"))
edited_line = input("> ")
if edited_line == ".":
new_file_list.append(item)
else:
new_file_list.append(edited_line + "\n")
edit = input("Do you want to save changes to the file? Y/N (default No): ")
if edit in ("Y", "y"):
with open(fname, "w") as fobj:
for item in new_file_list:
fobj.write(item)
print("File %s has been modified" % fname)
else:
print("File %s left unchanged" % fname)
# define main function
def main():
while True:
action = input("Enter read/create/edit as operating mode (exit to quit): ")
if action == "read":
read_file()
continue
elif action == "create":
write_file()
continue
elif action == "edit":
edit_file()
continue
elif action == "exit":
exit_prog()
else:
print("Unrecognized command: %s" % action)
continue
if __name__ == '__main__':
main()