473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python Doc Error: os.makedirs

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
7 4596
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

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
if not os.path.isdir("your_dir_name"):
makedirs("your_dir_name")

Oct 19 '05 #4
"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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Don Bruder | last post by:
Greetings, oh scaly ones... :) I'm a Mac user with a fairly high level of computer literacy, including reasonable programming skills in BASIC, C, Pascal, and several flavors of ML, just trying...
0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
2
by: paul h | last post by:
hi there, i'm learning to use python for cgi, and i've hit a few road blocks. basically, i need to know how to achieve the following things using python, on linux. create a new directory, and...
1
by: M.E.Farmer | last post by:
Hello c.l.py!, I have just finished this and decided to share. PySourceColor is a module to convert Python source into colored html. Yes it has been done before, but I like this better:) You can...
3
by: sumi | last post by:
How do i create a dir using python.........
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
3
by: Mike Orr | last post by:
I'm trying to install a program that uses Durus on a server. It appears that if a Python program uses eggs, it creates a ~/.python-eggs/ directory, so the home directory must be writeable. This...
6
by: Lex Hider | last post by:
Hi, Apologies if this is against etiquette. I've just got my first python app up and running. It is a podcast aggregator depending on feedparser. I've really only learnt enough to get this up and...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 414 open ( +1) / 3498 closed ( +9) / 3912 total (+10) Bugs : 949 open ( +6) / 6376 closed (+12) / 7325 total (+18) RFE : 247 open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.