List of WindowsError error codes and meanings

Andrew Berg bahamutzero8825 at gmail.com
Sun May 22 10:35:58 EDT 2011


On 2011.05.21 06:46 AM, John J Lee wrote:
> Since Python 2.5, the errno attribute maps the Windows error to error
> codes that match the attributes of module errno.
I was able to whip up a nifty little function that takes the output of
sys.exc_info() after a WindowsError and return the error code. I was
mistaken in my earlier post about sys.exc_info()[1][0] - I hadn't
noticed the 'WindowsError' bit before the opening parenthesis and
mistook it for a tuple (it's a WindowsError object). Anyway:

> def find_win_error_no(error_msg):
>     """
>     error_msg is a string with the error message - typically
> sys.exc_info()[1] from a WindowsError exception
>     Returns the number as a string, not an int
>     """
>     win_error_re = re.compile('\[Error \d{1,5}\]')
>     win_error_num_re = re.compile('\d{1,5}')
>     win_error = win_error_re.findall(error_msg)[0]
>     return win_error_num_re.findall(win_error)[0]
I call it here:
> def make_prog_dir(dir):
>     try:
>         os.mkdir(dir)
>     except WindowsError:
>         error_info = str(sys.exc_info()[1])
>         num = find_win_error_no(error_msg=error_info)
>         if num == '183': # Error 183 = 'Cannot create a file when that
> file already exists'
>             logger.debug(dir + ' exists.') # Ignore the error, but
> make a note
>         elif num == '5': # Error 5 = 'Access is denied'
>             logger.critical('Could not create', dir, '\(access denied\).')
>         else:
>             logger.critical('Could not create', dir, '-', error_info)
Errors 183 and 5 are going to be the most common, but I'll look at the
lists on MSDN to see if there's anything else worth adding (and
familiarize myself with any more common errors).



More information about the Python-list mailing list