Computer Science, asked by kalpeshvalvi675, 1 year ago

How to catch an exception while using a Python 'with' statement?

Answers

Answered by Aagmansriva993
0

To my shame, I can't figure out how to handle exception for python 'with' statement. If I have a code:

with open("a.txt") as f:

print f.readlines()

I really want to handle 'file not found exception' in order to do somehing. But I can't write

with open("a.txt") as f:

print f.readlines()

except:

print 'oops'

and can't write

with open("a.txt") as f:

print f.readlines()

else:

print 'oops'

enclosing 'with' in a try/except statement doesn't work else: exception is not raised. What can I do in order to process failure inside 'with' statement in a Pythonic way?

please mark me brainliest...

if you mark me brainliest

I also follow you...

Similar questions