[Tutor] useful function or reinventing the wheel??

Dave Angel d at davea.name
Tue Nov 29 07:22:53 CET 2011


On 11/29/2011 12:19 AM, Mark Lybrand wrote:
>>
>>
>>>   I couldn't follow your code, but finally concluded that it's trying to
>> create a directory, creating the directories parents also if they don't
>> exist either.  It would be much simpler if written recursively, but there's
>> no need.
>>
>> Check out os.makedirs(), and see if it meets your needs.   (that's
>> makedirs() function, in the os module)
>>
>>
> Okay, so I guess commenting is in order :)  "It was hard for me to write
> it, so it should be hard for you to read it." comes to mind.
>
> And I will probably try to write it recursively based on your suggestion,
> even though long term, I will just make use of os.makedirs, since that was
> what I was re-inventing.
>

You're welcome.  I'd look forward to seeing your rewrite, and whether 
it's really shorter and more straightforward.

Another advantage is doing less disk I/O if you start by trying the 
requested directory directly, and only recursing to parents if you can't 
make the requested directory.

I took a quick stab at it  (ask me later for mark.py)  and it wasn't as 
straightforward as I expected.  The main problem comes down to how to 
define the base case.  I think you might have the same problem also. 
You assume that you can safely go back to the mount point.  But if the 
path given is relative, you have to allow for that as well. Perhaps a 
call to os.path.abspath is in order.  I also think that ismount() might 
not be legal on Windows, if you care about that.

Also, we have to worry about what happens when one of the directories 
cannot be made for reasons unrelated to the syntax of the string.  For 
example, the user might not have write permissions for one or more of 
the directories.  In fact, the user might not have read permissions either.

-- 

DaveA


More information about the Tutor mailing list