Jul-14-2017, 08:22 PM
Don't ask for permission ask for forgiveness.
Instead of checking every possible possibility, you should use try: except:
Instead of checking every possible possibility, you should use try: except:
import sys filename = 'not_existing_file' try: fd = open(filename) except OSError as e: print(e, file=sys.stderr) sys.exit(1)Or you can be more concrete:
filename = '/dev/mem' try: fd = open(filename) except PermissionError as e: print('You don\'t have the permission to open the file {}'.format(filename), file=sys.stderr) sys.exit(1) except OSError as e: # PermissionError is a subclass of OSError # First you should be very concrete, and later # you can catch all other Errors # NEVER USE except: # this will catch all Errors print(e, file=sys.stderr) sys.exit(2)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!