Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Getting a WindowsError instead of an IOError

I am trying to understand exceptions with Python 2.7.6, on Windows 8.

Here's the code I am testing, which aims to create a new directory at My_New_Dir. If the directory already exists, I want to delete the entire directory and its contents, and then create a fresh directory.

import os

dir = 'My_New_Dir'
try:
    os.mkdir(dir)
except IOError as e:
    print 'exception thrown'
    shutil.rmtree(dir)
    os.mkdir(dir)

The thing is, the exception is never thrown. The code works fine if the directory does not already exist, but if the directory does exist, then I get the error:

WindowsError: [Error 183] Cannot create a file when that file already exists: 'My_New_Dir'

But according to the Python documentation for os.mkdir(),

If the directory already exists, OSError is raised.

So why is the Windows error being thrown, rather than the Python exception?

like image 556
Karnivaurus Avatar asked Feb 25 '14 22:02

Karnivaurus


People also ask

How do I get IOError in Python?

It is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. It is also raised for operating system-related errors.

What is WindowsError Python?

WindowsError is a subclass of OSError . From the exceptions documentation: Raised when a Windows-specific error occurs or when the error number does not correspond to an errno value.

What is the exception class in Python?

In Python, all exceptions must be instances of a class that derives from BaseException . In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived).

How can the try except statements handle errors in Python?

The Python try… except statement runs the code under the “try” statement. If this code does not execute successfully, the program will stop at the line that caused the error and the “except” code will run. The try block allows you to test a block of code for errors.


2 Answers

WindowsError is a subclass of OSError. From the exceptions documentation:

Raised when a Windows-specific error occurs or when the error number does not correspond to an errno value. The winerror and strerror values are created from the return values of the GetLastError() and FormatMessage() functions from the Windows Platform API. The errno value maps the winerror value to corresponding errno.h values. This is a subclass of OSError.

You are trying to catch IOError however, which is not such a parent class of WindowsError; as a result it won't suffice to catch either OSError nor WindowsError.

Alter your code to use the correct exception here:

try:
    os.mkdir(dir)
except OSError as e:

or use WindowsError; this would tie your code to the Windows platform.

like image 168
Martijn Pieters Avatar answered Sep 30 '22 18:09

Martijn Pieters


You mis-read it. It is "OSError" not "IOError", and WindowsError is a subclasss of "OSError" for your specific working OS.

>>> issubclass(WindowsError, OSError)
True

Besides, for your propose, this API is better:

os.path.isdir(path): Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.

if os.path.isdir(dir):
    shutil.rmtree(dir)
os.mkdir(dir)
like image 30
Sheng Avatar answered Sep 30 '22 17:09

Sheng