[Patches] [ python-Patches-1314067 ] os.makedirs - robust against partial path

SourceForge.net noreply at sourceforge.net
Fri Dec 2 21:11:17 CET 2005


Patches item #1314067, was opened at 2005-10-05 13:08
Message generated for change (Comment added) made by jimjjewett
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1314067&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Jim Jewett (jimjjewett)
Assigned to: Nobody/Anonymous (nobody)
Summary: os.makedirs - robust against partial path

Initial Comment:
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."

----------------------------------------------------------------------

>Comment By: Jim Jewett (jimjjewett)
Date: 2005-12-02 15:11

Message:
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).


----------------------------------------------------------------------

Comment By: Richard Barran (rbarran)
Date: 2005-12-01 11:39

Message:
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.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1314067&group_id=5470


More information about the Patches mailing list