Using python “with” statement with try-except block
问题 Is this the right way to use the python \"with\" statement in combination with a try-except block?: try: with open(\"file\", \"r\") as f: line = f.readline() except IOError: <whatever> If it is, then considering the old way of doing things: try: f = open(\"file\", \"r\") line = f.readline() except IOError: <whatever> finally: f.close() Is the primary benefit of the \"with\" statement here that we can get rid of three lines of code? It doesn\'t seem that compelling to me for this use case