By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Python Doc Error: os.makedirs

P: n/a
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
xa*@xahlee.org
http://xahlee.org/

Oct 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Oct 19 '05 #2

P: n/a

Xah Lee wrote:
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

Oct 19 '05 #3

P: n/a
if not os.path.isdir("your_dir_name"):
makedirs("your_dir_name")

Oct 19 '05 #4

P: n/a
"Xah Lee" <xa*@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, Linkping University, Sweden
"I refuse to have a battle of wits with an ! bellman @ lysator.liu.se
unarmed person." ! Make Love -- Nicht Wahr!
Oct 19 '05 #5

P: n/a
I wrote:
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
--
Thomas Bellman, Lysator Computer Club, Linkping University, Sweden
"Beware of bugs in the above code; I have ! bellman @ lysator.liu.se
only proved it correct, not tried it." ! Make Love -- Nicht Wahr!
Oct 19 '05 #6

P: n/a

Thomas Bellman wrote:
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
xa*@xahlee.org
http://xahlee.org/

Oct 20 '05 #7

P: n/a
On Wed, Oct 19, 2005 at 09:26:16AM -0700, Dr. Who wrote:
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-----

Oct 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.