Python Doc Error: os.makedirs

Thomas Bellman bellman at
Wed Oct 19 21:53:00 CEST 2005

"Xah Lee" <xah at> wrote:

> The "Throws an error exception" should be "Throws an OSError
> exception".

Both are correct:

    >>> os.error is OSError

That is even documented in

	This exception is raised when a function returns a system-related
	error (not for illegal argument types or other incidental errors).
	This is also known as the built-in exception OSError.

> i think the function shouldn't complain if dir already exists. How is a
> programer to distinguish if the dir already exists, or if there's a
> problem creating the dir?

Also in the documentation about os.error:

	The accompanying value is a pair containing the numeric error
	code from errno and the corresponding string, as would be printed
	by the C function perror().  See the module errno, which contains
	names for the error codes defined by the underlying operating

	When exceptions are classes, this exception carries two attributes,
	errno and strerror.  The first holds the value of the C errno
	variable, and the latter holds the corresponding error message
	from strerror().  For exceptions that involve a file system path
	(such as chdir() or unlink()), the exception instance will
	contain a third attribute, filename, which is the file name
	passed to the function.

Thus, this gives you the behaviour you want:

    except os.error, e:
	if e.errno != errno.EEXIST:

Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"I refuse to have a battle of wits with an   !  bellman @
 unarmed person."                            !  Make Love -- Nicht Wahr!

More information about the Python-list mailing list