How do you test file.read () error in Python?

I have the following code (adapted from the example given in Dive Into Python ) that reads the entire contents of a file into a buffer.

buffer = "" try: file = open(postFileName, 'rU') try: # Read the entire POST log file into a buffer buffer += file.read() finally: file.close() except IOError: buffer += "The POST file could not be opened." 

What bothers me about this code is the internal try / finally block without the except block. Do I need some kind of block? Can the read() call fail after the open() call completes successfully? I know that try-except-finally is now unified, so adding one, at least syntactically, is not a problem.

If I add an exception block, under what conditions will it be executed, and how can I write a test to make sure that it works under these conditions?

Also, if I don't need an except block, then why do I need an internal try / finally block?

+4
source share
4 answers

I find that finally, blocks are often abused. Closing the file (and several other similar patterns) is so important that Python 3.0 will have a c operator to cover this base in a slightly less obscure way.

  • Do I need, except with the ending?

    This affects the confusing nature of this particular example and why they added operator c .

    finally performs "no matter what" clears. An exception or exception is finally always executed.

  • Can the call to read () end after the call to the open () function was successful?

    All OS calls, all I / O calls (almost all) can throw an exception. All kinds of bad things can happen after opening and before reading.

  • If I add an except block, under what conditions will it be executed?

    Reading files. There are many I / O errors that can occur between open and read. Also, read the built-in exceptions. https://docs.python.org/2/library/exceptions.html

  • How to write a test to make sure that it works in these conditions?

    You will need a mock file. This object will respond to open , but raises an IOError or OSError on every read .

  • If I don't need an except block, then why do I need an internal try / finally block?

    Cleanup will finally be executed no matter what exception is thrown.

Try it. See what he does.

 try: raise OSError("hi mom") finally: print "Hmmm" 
+6
source

I disagree with the other answers that mention the union of try / except / finally blocks. This would change the behavior since you would not want the finally block to try to close the file if the failure failed. The split blocks are correct here (although it is better to use the new syntax < with open(filename,'rU') as f ).

There are reasons why read () may fail. For example, the data may be too large to fit in memory, or the user could signal an interrupt with the -C control. These cases will not be caught by an IOError, but they will be handled (or not) by the caller, who may want to do different things depending on the nature of the application. However, the code still has an obligation to clear the file, even if it does not deal with the error, therefore, finally, with no exceptions.

+3
source

With the recent version of Python, you do not need to embed try-except and try-finally. try-except-finally has been unified:

 try: non_existing_var except: print 'error' finally: print 'finished' 
0
source

Along the way, it turned out a little Unified try / except / finally

Hope this helps;)

0
source

All Articles