Python Doc Error: os.makedirs

X

Xah Lee

Python doc problem:

http://python.org/doc/2.4.2/lib/os-file-dir.html

makedirs( path[, mode])
Recursive directory creation function. Like mkdir(), but makes all
intermediate-level directories needed to contain the leaf directory.
Throws an error exception if the leaf directory already exists or
cannot be created. The default mode is 0777 (octal). This function does
not properly handle UNC paths (only relevant on Windows systems;
Universal Naming Convention paths are those that use the `\\host\path'
syntax). New in version 1.5.2.


The “Throws an error exception†should be “Throws an OSError
exceptionâ€.

--------

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?


Xah
(e-mail address removed)
∑ http://xahlee.org/
 
P

pclinch

if os.access( path, os.F_OK):
print 'path exists'

See also os.stat(path) for further info. about a file or dir.

regards, Paul Clinch
 
D

Dr. Who

Xah said:
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?

Of course it should thrown an exception because it was unable to do
what it was asked: create a directory. The fact that the directory
already exists is irrelevant to the function...it still failed to
create the directory.

And I have had situations where attempting to create a directory that
already exists was an error condition.

Jeff
 
T

Thomas Bellman

Xah Lee said:
The "Throws an error exception" should be "Throws an OSError
exception".

Both are correct:
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
 
T

Thomas Bellman

I said:
try:
os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
except os.error, e:
if e.errno != errno.EEXIST:
raise

Actually, when I think more about it, one would probably rather
want something like:

try:
os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
except os.error, e:
if ( e.errno != errno.EEXIST or
not os.path.isdir("/tmp/trh/spam/norwegian/blue/parrot/cheese")):
raise
 
X

Xah Lee

Thomas said:
try:
os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
except os.error, e:
if e.errno != errno.EEXIST:
raise

This is what i want. Thanks.

(the doc needs quite some improvement...)

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
J

jepler

The fact that the directory already exists is irrelevant to the function...it
still failed to create the directory.

That's not true. Imagine that os.makedirs() is used inside tempfile.mkdtemp()
(I looked, and it isn't) and the proposed behavior (do not raise an exception
when the directory already exists) is adopted.

In this case, there is a race condition between you and the attacker who
guesses the next directory you will attempt to make. If he calls mkdir()
before you do, then your os.makedirs() returns successfully (instead of raising
an exception) and you place your files into a location that is under the
control of someone else.

If the attacker then makes the directory setuid himself, that files created in
the directory are owned by him. Now, he can view and change the contents of
these files. This can lead to a local priviledge escalation.

Errors should never pass silently.
Unless explicitly silenced.
-- from the Zen of Python ('import this')
.... and wanting them to do so may introduce a security bug in your software.

If you know more about your users and their environments than I do (for
instance, that none of them will ever use a multi-user computer system) maybe
you should choose to wrap os.makedirs with something that silences EEXIST.
But I'm glad Python does the secure thing and treats EEXIST as a failure by default.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDWDrkJd01MZaTXX0RAoaOAJ9W1IlFBH7lFLC/izH2S1euV87xGgCggR81
eeK3wBR5X6DvbXZcIX0NMoA=
=dzVp
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top