Python Doc Error: os.makedirs
Thomas Bellman
bellman at lysator.liu.se
Wed Oct 19 15:53:00 EDT 2005
"Xah Lee" <xah at xahlee.org> wrote:
> The "Throws an error exception" should be "Throws an OSError
> exception".
Both are correct:
>>> os.error is OSError
True
That is even documented in http://python.org/doc/lib/module-os.html:
error
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
system.
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:
try:
os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
except os.error, e:
if e.errno != errno.EEXIST:
raise
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"I refuse to have a battle of wits with an ! bellman @ lysator.liu.se
unarmed person." ! Make Love -- Nicht Wahr!
More information about the Python-list
mailing list