#!/usr/bin/python """ files.py -- a few file-related operations, plus a first encounter with exceptions Jeff Ondich, 24 October 2006 Read, predict the output, experiment. """ import sys print '=============== command-line arguments ==============' if len(sys.argv) <= 1: sys.stderr.write('Usage: %s filename\n' % sys.argv[0]) sys.exit(1) fileName = sys.argv[1] print 'The requested file is', fileName print print '=============== reading from a file ==============' try: theFile = open(fileName) except Exception, e: sys.stderr.write('Here is the error message built into the exception:\n %s\n' % e) sys.exit(1) for line in theFile: line = line.upper() print line, theFile.close() print print '=============== writing to a file ==============' try: theFile = open('output.txt', 'w') except Exception, e: sys.stderr.write('Here is the error message built into the exception:\n %s\n' % e) sys.exit(1) theFile.write('the moose frolicked\n') theFile.write('in the meadow\n') theFile.close() print