473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4615
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.mkdtem p()
(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)

iD8DBQFDWDrkJd0 1MZaTXX0RAoaOAJ 9W1IlFBH7lFLC/izH2S1euV87xGgC ggR81
eeK3wBR5X6DvbXZ cIX0NMoA=
=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
1525
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 to run a "canned" Python... Uhhh... Program? Script? Module? - Yeesh... I'm such a rookie at Python I'm not even sure of the right terminology to use here... At this point in the game, I'm not actually trying to *DO* anything in Python, I just...
0
2416
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
2
2288
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 sub-directory. so if the user inputs "HELLO", i can create a dir called HELLO, and then some other dirs in there. i also need to know how i can write various things into a file. the following doesnt seem to work for me:
1
2577
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 easily define your own colorscheme. example usage: # Highlight PySourceColor.py python PySourceColor.py or # Show help
3
2247
by: sumi | last post by:
How do i create a dir using python.........
5
6779
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 cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter between files, maybe a homemade version of truecrypt could be constructed. Any idea what it...
3
4830
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 conflicts with server environments where you want to run a daemon with minimum privileges. Second, it appears to use the real user ID rather than the effective user ID to choose the home directory. In this case I'm trying to use start-stop-daemon...
6
1915
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 running. Any tips on the code quality and use of python would be appreciated. I've got a feeling the overall structure is up the creek. approx 220 LOC. file: GodCast.py
0
1048
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 ( +1) / 245 closed ( +1) / 492 total ( +2) New / Reopened Patches ______________________
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.