Simon Gerber wrote:
> I've got this:
> 
> -------
> def isBottomDir(path):
>     for item in os.listdir(path):
>         if os.path.isdir(os.path.join(path,item)):
>             return False
>     return True
> -------
> 
> Is that an acceptable way of doing this? I've been reading
> http://thedailywtf.com for a month or so now - and though I've yet to
> see a Python example cross their pages, I'm rather terrified of
> becoming the first :)

It's fine, relax :-)
> 
> To iterate through the directories, the following works for me:
> 
> -------
> for root, dirs, files in os.walk('~\Test'):
>     if test_dir.isBottomDir(root) == True: print root
> -------
> 
> Is it worthwhile trying to write my own recursive function for this?
> Or can I safely leave this task to to os.walk without inviting
> ridicule from the programming community.

Use os.walk(), reinventing the wheel is more likely to invite ridicule ;)

But note the three values you get from os.walk() - the current dir 
(root), a list of dirs in root, a list of files in root...hmm, do you 
really need to call isBottomDir()?
> 
>>There's a module that does this called 'optparse':
>>
>>    http://www.python.org/doc/lib/module-optparse.html
>>
>>Try reusing that first, because it's been fairly well exercised and tuned
>>to what people expect from an option parser.
> 
> 
> Excellent. That looks like what I was looking for.

There is a great cookbook recipe that makes using optparse as easy as 
writing a "usage" string:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278844

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to