python – specifically handle file exists exception

According to the code print ..., it seems like you’re using Python 2.x. FileExistsError was added in Python 3.3; You can’t use FileExistsError.

Use errno.EEXIST:

import os
import errno

try:
    os.mkdir(folderPath)
except OSError as e:
    if e.errno == errno.EEXIST:
        print('Directory not created.')
    else:
        raise

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)