[Tutor] Finding bottom level directories, and command-line arguments

Simon Gerber nequeo at gmail.com
Wed Feb 8 02:54:31 CET 2006


Hi Danny,

Thanks for the advice.

> It might be interesting to write a function that takes an arbitrary
> directory, and returns 'True' if that directory is a bottom-level
> directory.  Can you write this function?

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

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.

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

Thanks!

--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
    screensavers'


More information about the Tutor mailing list