"list file tree with os walkimport sysos def lister(root)for (thisdirsubsherefilesherein os walk(root)print('[thisdir ']'for fname in filesherepath os path join(thisdirfnameprint(pathif __name__ ='__main__'lister(sys argv[ ]for root dir generate dirs in tree print files in this dir add dir name prefix dir name in cmdline when packaged this waythe code can also be run from shell command line here it is being launched with the root directory to be listed passed in as command-line argumentc:\pp \system\filetoolspython lister_walk py :\temp\test [ :\temp\testc:\temp\test\random bin :\temp\test\spam txt :\temp\test\temp bin :\temp\test\temp txt [ :\temp\test\partsc:\temp\test\parts\part :\temp\test\parts\part :\temp\test\parts\part :\temp\test\parts\part here' more involved example of os walk in action suppose you have directory tree of files and you want to find all python source files within it that reference the mime types module we'll study in the following is one (albeit hardcoded and overly specificway to accomplish this taskimport os matches [for (dirnamedirsherefilesherein os walk( ' :\temp\pp \examples')for filename in fileshereif filename endswith(py')pathname os path join(dirnamefilenameif 'mimetypesin open(pathnameread()matches append(pathnamefor name in matchesprint(namec:\temp\pp \examples\pp \internet\email\mailtools\mailparser py :\temp\pp \examples\pp \internet\email\mailtools\mailsender py :\temp\pp \examples\pp \internet\ftp\mirror\downloadflat py :\temp\pp \examples\pp \internet\ftp\mirror\downloadflat_modular py :\temp\pp \examples\pp \internet\ftp\mirror\ftptools py :\temp\pp \examples\pp \internet\ftp\mirror\uploadflat py :\temp\pp \examples\pp \system\media\playfile py file and directory tools