Using python statement with statement with try-except block

Is it correct to use a python statement with an instruction in combination with a try-except block ?:

try: with open("file", "r") as f: line = f.readline() except IOError: <whatever> 

If so, then, given the old way of doing things:

 try: f = open("file", "r") line = f.readline() except IOError: <whatever> finally: f.close() 

Is the main advantage of the "with" expression here, that we can get rid of three lines of code? It seems to me that this is not convincing for me for this use case (although I understand that the "c" operator has other uses).

EDIT: Is the functionality of these two blocks of code the same?

EDIT2: The first few answers talk mostly about the benefits of using β€œc,” but they seem to be the ultimate benefit here. We were all (or should have) explicitly called f.close () for years. I believe that one advantage is that sloppy encoders will benefit from the use of "c".

+73
python try-catch with-statement finally except
Sep 04 2018-10-11T00:
source share
4 answers
  • The two codes you provided are not equivalent
  • The code that you described as the old way of what is happening has a serious error: if you open the file, you will not be able to get the second exception in finally , because f not an estimate.

The equivalent old style code would be:

 try: f = open("file", "r") try: line = f.readline() finally: f.close() except IOError: <whatever> 

As you can see, the with statement can make things less error prone. In newer versions of Python (2.7, 3.1), you can also combine multiple expressions in a single with statement. For example:

 with open("input", "r") as inp, open("output", "w") as out: out.write(inp.read()) 

In addition, I personally consider it a bad habit to catch any exception as early as possible. This is not the purpose of the exception. If an I / O function that may fail is part of a more complex operation, in most cases an IOError must abort the entire operation and therefore be handled externally. Using with statements, you can get rid of all these try...finally at internal levels.

+111
Sep 05 '10 at 1:20
source share

If the contents of the finally block are determined by the properties of the open file object, why shouldn't the developer of the file object write the finally block? This is an advantage of the with statement, much more than storing three lines of code in this particular case.

And yes, the way you combined with and try-except is the only way to do this, because exceptional errors raised in the open statement itself cannot be detected within the with block.

+6
Sep 04 2018-10-11T00:
source share

I think you were mistaken in the statement that this only reduces the number of lines. It actually performs initialization and manual stall.

In your case, "c" does

  • open the file
  • processes its contents and
  • be sure to close it.

Here is a link to understand the "with" statement: http://effbot.org/zone/python-with-statement.htm

Edit: Yes, the correct use of β€œc” and the functionality of both blocks of code are the same. The question is why use "c"? this is because of the benefits you get with this. as you mentioned the randomly missing f.close ().

+1
Sep 04 '10 at
source share

The larger the Pythonic method for the following codes:

 try: f = open("file", "r") try: line = f.readline() finally: f.close() except IOError: <whatever> try: f = open("file", "r") except IOError: <whatever> else: f.close() 
-3
Oct. 13 '15 at 21:21
source share



All Articles