How can I make an error verifiy with os.makedirs in Python?

try:
    os.makedirs('C:\\test\\')
except OSError:
    pass

You also might want to check the specific "already exists" error (since OSError could mean other things, like permission denied...

import errno
try:
    os.makedirs('C:\\test\\')
except OSError as e:
    if e.errno != errno.EEXIST:
        raise  # raises the error again

In Python3.2 and above, just add exist_ok=True will solve this problem.

If exist_ok is False (the default), an FileExistsError is raised if the target directory already exists.

os.makedirs('C:\\test\\',exist_ok=True)

Tags:

Python