15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1 #!/usr/bin/env python<br />

2<br />

3 'readTextFile.py -- read and display text file'<br />

4<br />

5 # get filename<br />

6 fname = raw_input('Enter filename: ')<br />

7 print<br />

8<br />

9 # attempt to open file for reading<br />

10 try:<br />

11 fobj = open(fname, 'r')<br />

12 except IOError, e:<br />

13 print "*** file open error:", e<br />

14 else:<br />

15 # display contents to the screen<br />

16 for eachLine in fobj:<br />

17 print eachLine,<br />

18 fobj.close()<br />

In other words, we are performing the validation elsewhere (if at all). Line 7 just displays a new line to<br />

separate the prompting of the filename and the contents of the file.<br />

Lines 918<br />

This next <strong>Python</strong> construct (other than the comment) represents the rest of the script. This is a tryexcept-else<br />

statement. The try clause is a block of code that we want to monitor for errors. In our code<br />

(lines 1011), we are attempting to open the file with the name the user entered.<br />

The except clause is where we decide what type of errors we're looking out for and what to do if such<br />

errors occur. In this case (lines 1213), we are checking to see if the file open() failedthis is usually an<br />

IOError type of error.<br />

Finally, lines 1418 represent the else clause of a try-exceptthe code that is executed if no errors<br />

occurred in the TRy block. In our case, we display each line of the file to the screen. Note that because<br />

we are not removing the trailing whitespace (line termination) characters from each line, we have to<br />

suppress the NEWLINE that the print statement automatically generatesthis is done by adding a trailing<br />

comma to the end of the print statement. We then close the file (line 18), which ends the program.<br />

One final note regarding the use of os.path.exists() and an exception handler: The author is generally<br />

in favor of the former, when there is an existing function that can be used to detect error conditionsand<br />

even more simply, where the function is Boolean and gives you a "yes" or "no" answer. (Note that there<br />

is probably already an exception handler in such a function.) Why do you have to reinvent the wheel<br />

when there's already code just for that purpose?<br />

An exception handler is best applied when there isn't such a convenient function, where you the<br />

programmer must recognize an "out of the ordinary" error condition and respond appropriately. In our<br />

case, we were able to dodge an exception because we check to see if a file exists, but there are many<br />

other situations that may cause a file open to fail, such as improper permissions, the connection to a<br />

network drive is out, etc. For safety's sake, you may end up with "checker" functions like os.path.exists<br />

() in addition to an exception handler, which may be able to take care of a situation where no such<br />

function is available.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!