9
import os

for dirpaths, dirnames, filenames in os.walk(mydir):
    print dirpaths

gives me all (sub)directories in mydir. How do I get only the directories at the very bottom of the tree?

djas
  • 973
  • 8
  • 24
  • 1
    Recursively go all the way down and don't come back? – squiguy Jul 30 '13 at 19:32
  • 1
    "very bottom" may not be meaningful depending on how you have your directories structured. – roippi Jul 30 '13 at 19:36
  • 1
    Have you tried anything? – Hai Vu Jul 30 '13 at 19:37
  • 1
    Can you show and example of a directory tree and the output you would like to achieve? – Racso Jul 30 '13 at 19:37
  • By 'very bottom' do you mean the end of each path tree? – dawg Jul 30 '13 at 19:39
  • It seems the OP means "leaf dirs" instead of "bottom-most dirs"--There is a distinction between the two. – Hai Vu Jul 30 '13 at 19:50
  • Thanks you all (well, not all) for your comments. I didn't know about the distinction between bottom-most and leaf dirs. @Hai yes, I did try stuff -- I might not be smart, but am not lazy, thanks. – djas Jul 30 '13 at 20:00
  • I am not accusing. In general, please show what you tried so people can help you. – Hai Vu Jul 30 '13 at 20:03
  • I agree with @HaiVu. We don't judge or accuse on SO, but it makes our jobs a lot easier if you show us what you've attempted. That way, we are less vulnerable to misunderstandings from terminology (one of the questions was "what is a down-most directory?"); further, we might also be able to highlight where your implementation diverges from your spec. Posting your attempts is simply a tool to explain things better to you and also to show you where you may have gone wrong in the first place. Take [this](http://stackoverflow.com/q/6470428/198633) as an example of how to formulate a question – inspectorG4dget Jul 30 '13 at 20:20
  • @HaiVu: could you please expand on the distinction between bottom-most dirs and leaf dirs? I understood them to be the same, which according to your above comment is incorrect. – inspectorG4dget Jul 30 '13 at 20:22
  • @inspectorG4dget: I tried to follow the guidelines with commonsense. And apparently it worked, since I got two spot on answers within minutes. Yes, I was vague on the definition of a "down-most" or "very bottom" dir. Yes, I could have spent an extra 5 mins building an example of what I was attempting, but most likely in my example leaf dirs would also be the deepest dirs, so both you and I would remain ignorant on this distinction. Some ambiguity sometimes is harmless -- and inevitable unless we are doing maths or physics, which we aren't. – djas Jul 31 '13 at 02:12
  • @inspectorG4dget: See my answer. Bottom most=leaf at max depth. – Hai Vu Jul 31 '13 at 02:26

4 Answers4

18

This will print out only those directories that have no subdirectories within them

for dirpath, dirnames, filenames in os.walk(mydir):
    if not dirnames:
        print dirpath, "has 0 subdirectories and", len(filenames), "files"
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
4

Like this?

for dirpaths, dirnames, filenames in os.walk(mydir):
    if not dirnames: print dirpaths
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
2

I saw two solutions for showing leaf directories (i.e. those that does not contain sub-dir). Bottom-most directories, on the other hand, are not only leaf directories, but also are at the maximum depth. Here is a crude way to figure out the bottom-most directories:

import os

mydir = '/Users/haiv/src/python'
max_depth = 0
bottom_most_dirs = []
for dirpath, dirnames, filenames in os.walk(mydir):
    depth = len(dirpath.split(os.sep))
    if max_depth < depth:
        max_depth = depth
        bottom_most_dirs = [dirpath]
    elif max_depth == depth:
        bottom_most_dirs.append(dirpath)
print bottom_most_dirs
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
1
import os

mydir='/home/'
print [dirpaths for dirpaths, dirnames, filenames in os.walk(mydir) if not dirnames]