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?
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.
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.
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).
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.
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. Thewinerror
andstrerror
values are created from the return values of theGetLastError()
andFormatMessage()
functions from the Windows Platform API. Theerrno
value maps thewinerror
value to correspondingerrno.h
values. This is a subclass ofOSError
.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With