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?
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"
Like this?
for dirpaths, dirnames, filenames in os.walk(mydir):
if not dirnames: print dirpaths
import os
mydir='/home/'
print [dirpaths for dirpaths, dirnames, filenames in os.walk(mydir) if not dirnames]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With