[Tutor] Read-create-edit file

biboy mendz bibsmendez at gmail.com
Sun Dec 13 20:35:19 CET 2009


Hi list,

I finally manage to sort of complete my read-create-edit.py file (after 
finding precious free time). I appreciate your review and good/bad 
comments to my code. It look and runs ok from my side here :-)

#read_create_edit.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Exercise 3.12: combined read, edit and write program
use import to utilize codes from other module files
Core Python Programming 2nd Edition by Wesley Chun
Filename: read_create_edit.py
Created: 13-Dec-2009
'''

from readFile import main as read_main
from readFile import repeat
from createFile1 import main as create_main
from appendFile import main as edit_main

def main():
'''heart of the program'''
while True:
print '''
(1) read file
(2) create file
(3) edit file (add data)
(4) quit program
'''
try:
ask = int(raw_input('choose your option: '))
if ask == 1:
read_main()
elif ask == 2:
create_main()
elif ask == 3:
edit_main()
elif ask == 4:
break
else:
print 'invalid choice: enter (1), (2), (3) or (4) only!'
except Exception, e:
print 'Error detected: ', e

if __name__ == '__main__':
main()
ask = repeat()
if ask == 'y':
main()
else:
print 'thank you for using python, goodbye!!'

#appendFile.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Chapter 3.1 :
Core Python Programming 2nd Edition by Wesley Chun
Filename: appendFile.py
Created: 09-Dec-2009
v1.0: append user input to existing file
'''

import os, sys

def main():
while True:
fname = raw_input('Enter filename to APPEND/edit: ')
if os.path.isdir(fname):
print('%s is a directory!') % fname
elif not os.path.exists(fname):
print 'this is APPEND mode; select CREATE to create new file'
elif fname.isspace():
print 'Space is not allowed'
else:
#os.path.exists(fname):
#found candidate to edit
break

all = [] # container list to hold user input lines
print("file '%s' will be appended by your input\n") % fname
quit_prompt = "[start your input to quit enter 1 dot]--> "

while True:
entry = raw_input(quit_prompt)
if entry == '.':
break
else:
all.append(entry)

confirm = raw_input('save file to disk?(y/N)')
confirm = confirm.lower() #convert to lowercase
if confirm == 'y':
fobj = open(fname, 'a')
fobj.write('\n'.join(all) + '\n') #(a)
fobj.close()
print('DONE! open %s to view your file') % fname
else:
print 'not saving to disk!'

if __name__ == '__main__':
main()
#(a) as per help from tutor list

#readFile.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Exercise 3.10b: read text file line by line; raise error if file not found,
encapsulate program body with main function for code re-use

Core Python Programming 2nd Edition by Wesley Chun
Filename: readFile.py
Created: 01-Dec-2009
edited: 07-Dec-2009
Version1: replace try-except with os.path.exists()
'''

import os

def repeat():
'''ask to repeat or no'''
ask = raw_input('\ntry again? [N/y]')
ask = ask.lower()
return ask

def main():
while True:
fname = raw_input('enter file name to READ: ')
if os.path.isdir(fname):
print('%s is a directory!') % fname
elif not os.path.exists(fname):
print 'Error: file not found!'
else:
fobj = open(fname, 'r')
for eachline in fobj:
print eachline,
fobj.close()
break

if __name__ == '__main__':
main()

#createFile.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Chapter 3.1 : create a file and write to disk
Core Python Programming 2nd Edition by Wesley Chun
Filename: createFile1.py
Created: 07-Dec-2009
rev. 1: fine tune script by checking if input name exist, is a file or a 
directory
'''

import os, sys

def main():
while True:
fname = raw_input('Enter filename to CREATE: ')
if os.path.isdir(fname):
print('%s is a directory!') % fname
elif os.path.exists(fname):
print '%s already exist' % fname
elif fname.isspace():
print 'Space is not allowed'
else:
break

all = [] # container list to hold user input lines
print("\nPopulate your file *%s*! \n") % fname
quit_prompt = "[to quit enter a dot '.' by itself]--> "

while True:
entry = raw_input(quit_prompt)
if entry == '.':
break
else:
all.append(entry)

confirm = raw_input('save file to disk?(y/N)')
confirm = confirm.lower() #convert to lowercase
if confirm == 'y':
fobj = open(fname, 'w')
fobj.write('\n'.join(all))
fobj.close()
print('DONE! open %s to view your file') % fname
else:
print 'not saving to disk!'

if __name__ == '__main__':
main()




-- 
Regards,
bibs M.

Host/Kernel/OS  "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 
[sidux 2009-02 Αιθήρ - kde-full - (200907141427) ]
www.sidux.com



More information about the Tutor mailing list