This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.makedirs - robust against partial path
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, jimjjewett, rbarran
Priority: normal Keywords: patch

Created on 2005-10-05 17:08 by jimjjewett, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
os.py.patch jimjjewett, 2005-10-05 17:08 os.py patch - more robust makedirs
Messages (5)
msg48837 - (view) Author: Jim Jewett (jimjjewett) Date: 2005-10-05 17:08
os.py function makedirs is intended to create a 
directory, including any required parent directories.

Unfortunately, at least on windows, it fails is some of 
those parent directories already exist.  This patch 
says "if the directory I'm about to create is already an 
existing directory, then pretend I succeeded and 
continue with the next step."
msg48838 - (view) Author: Richard Barran (rbarran) Date: 2005-12-01 16:39
Logged In: YES 
user_id=1207189

Hi,

Could you provide some example code that shows up the error?
If I understand you correctly, the following should fail:

import os
os.mkdir('c:/temp/a')
os.makedirs('c:/temp/a/b/c')

But it works fine on my WinXP pro SP2 machine.
msg48839 - (view) Author: Jim Jewett (jimjjewett) Date: 2005-12-02 20:11
Logged In: YES 
user_id=764593

slight misdiagnosis on my part -- it only fails if the 
*entire* directory tree already exists.  
 
"""
>>> os.makedirs('c:/temp/a/b/c')

>>> os.makedirs('c:/temp/a/b/c')

Traceback (most recent call last):
  File "<pyshell#64>", line 1, in -toplevel-
    os.makedirs('c:/temp/a/b/c')
  File "C:\Python24\lib\orig_os.py", line 159, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: 'c:/temp/a/b/c'
"""

My use case was generating java files in the proper package 
- and possibly regenerating them if something changed.  I 
want to put them in the right directory, which will usually 
(but not always) already exist.

The patch still works, but I now wonder if it might be 
better to put a guard at the top along the lines of "if 
path.exists(name): return" (or something fancier to ensure 
that it is a directory with appropriate permissions).
msg48840 - (view) Author: Richard Barran (rbarran) Date: 2005-12-05 12:39
Logged In: YES 
user_id=1207189

I think that the current behavior of the function is correct.
It tries to mimic os.mkdir - if the dir to create ("c" in
your example) already exists, it will raise an error.
However, errors on intermediate dirs are ignored.
This makes sense, but I think the documentation is not clear
on this (or maybe I'm just slow :-)
BTW - there's another patch ([ 1239890 ] Prevent race
condition in os.makedirs) to rewrite the whole function and
it might make your patch in its current form obsolete.
Maybe your work can be merged into this other patch?
msg48841 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-12-09 09:26
I agree with rbarran. Closing as Rejected.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42450
2005-10-05 17:08:06jimjjewettcreate