11#!/usr/bin/env python3
22
3- 'textFile.py -- create/display text files'
3+ 'textFile.py -- create/display/edit text files'
44
55import sys ,os
66from os import linesep as ls
77from shutil import get_terminal_size as term_size
88
99# calculate number of # to fill term width
10- prg_dialogue = "Enter file name (back to return to main screen): "
1110nb_hash = term_size ()[0 ]
1211hash_line = "#" * nb_hash
12+ prg_dialogue = "Enter file name ('back' to return to main screen, 'exit' to exit program): "
13+
14+ # function to exit program
15+ def exit_prog ():
16+ print ("Program exit!" )
17+ sys .exit (0 )
1318
1419# function to create file
1520def write_file ():
@@ -20,6 +25,8 @@ def write_file():
2025 print ("Going back to main program" )
2126 create_file = False
2227 break
28+ elif fname == "exit" :
29+ exit_prog ()
2330 elif os .path .exists (fname ):
2431 print ("ERROR: %s already exists" % fname )
2532 else :
@@ -52,6 +59,8 @@ def read_file():
5259 if fname == "back" :
5360 print ("Going back to main program" )
5461 break
62+ elif fname == "exit" :
63+ exit_prog ()
5564 elif not os .path .exists (fname ):
5665 print ("ERROR: %s doesn't exist" % fname )
5766 continue
@@ -72,6 +81,8 @@ def edit_file():
7281 if fname == "back" :
7382 print ("Going back to main program" )
7483 break
84+ elif fname == "exit" :
85+ exit_prog ()
7586 elif not os .path .exists (fname ):
7687 print ("ERROR: %s doesn't exist" % fname )
7788 continue
@@ -113,8 +124,7 @@ def main():
113124 edit_file ()
114125 continue
115126 elif action == "exit" :
116- print ("Program exit!" )
117- sys .exit (0 )
127+ exit_prog ()
118128 else :
119129 print ("Unrecognized command: %s" % action )
120130 continue
0 commit comments