Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5977

Re: List of WindowsError error codes and meanings

Date 2011-05-22 09:35 -0500
From Andrew Berg <bahamutzero8825@gmail.com>
Subject Re: List of WindowsError error codes and meanings
References <mailman.1847.1305914213.9059.python-list@python.org> <8762p48eel.fsf@pobox.com>
Newsgroups comp.lang.python
Message-ID <mailman.1916.1306074967.9059.python-list@python.org> (permalink)

Show all headers | View raw


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).

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-20 12:56 -0500
  Re: List of WindowsError error codes and meanings Genstein <genstein@invalid.invalid> - 2011-05-20 20:47 +0100
    Re: List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-20 16:55 -0500
  Re: List of WindowsError error codes and meanings John J Lee <jjl@pobox.com> - 2011-05-21 12:46 +0100
    Re: List of WindowsError error codes and meanings Genstein <genstein@invalid.invalid> - 2011-05-21 16:35 +0100
      Re: List of WindowsError error codes and meanings John Lee <jjl@pobox.com> - 2011-05-22 17:55 +0000
    Re: List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-22 09:35 -0500
  Re: List of WindowsError error codes and meanings Thomas Heller <theller@ctypes.org> - 2011-05-26 17:02 +0200
    Re: List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-27 04:22 -0500

csiph-web