OS.Walk()OS.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
root : Prints out directories only from what you specified dirs : Prints out sub-directories from root. files: Prints out all files from root and directories
Making a script
Having that information we can create a simple script doing just that. This script will print out all directories, sub-directories and files from the path I specified (/var/log)
import os print "root prints out directories only from what you specified" print "dirs prints out sub-directories from root" print "files prints out all files from root and directories" print "*" * 20 for root, dirs, files in os.walk("/var/log"): print root print dirs print files
The second examples extends the first one with showing how much every file consumes using the getsize function.
print "This is using getsize to see how much every file consumes" print "---------------" from os.path import join, getsize for root, dirs, files in os.walk('/tmp'): print root, "consumes", print sum([getsize(join(root, name)) for name in files]), print "bytes in", len(files), "non-directory files"
Read more about: