Skip to content Skip to sidebar Skip to footer

Python Continue With Execution In Case Of Exception

I am trying to continue with my code eventhough exception is present. Just print the exception and continue with code. Below is sample : def mkdir(path): mypath = './cust

Solution 1:

Need to import errno module.

The errno module defines a number of symbolic error codes

Solution 2:

You forgot to import errorno

import os
import errno


defmkdir(path):
    mypath = "./customers/" + path
    print(mypath)
    try:
        os.makedirs(mypath)
    except OSError as exc:
        print(exc.errno)
        if exc.errno == errno.EEXIST and os.path.isdir(mypath):
            passif __name__ == '__main__':
    item = 'dev'
    mkdir(item)
    print("Done")

Solution 3:

Just do:

except OSError:
    pass

or except Exception to except all exceptions (albeit not ideal). You are trying to use a module without importing (and I don't think you need to import errno for this script).

Post a Comment for "Python Continue With Execution In Case Of Exception"