Walking the Directory Tree : path « File « Python Tutorial






import os
path = "/books/python"

def printFiles(dirList, spaceCount):
    for file in dirList:
        print "/".rjust(spaceCount+1) + file

def printDirectory(dirEntry):
    print dirEntry[0] + "/"
    printFiles(dirEntry[2], len(dirEntry[0]))

tree = os.walk(path)
for directory in tree:
    printDirectory(directory)








12.21.path
12.21.1.Module Attributes to Aid in Multi-platform Development
12.21.2.os.path.abspath(path) returns a string version of the absolute path of the path specified
12.21.3.os.path module provides exists(path), isdir(path), and isfile(path) to check for the existence of files and directories
12.21.4.os.chdir(path) changes the current working directory
12.21.5.Walking the Directory Tree
12.21.6.Searching for Files Based on Extension
12.21.7.Working with Directories
12.21.8.Splitting Pathnames
12.21.9.Listing Directories
12.21.10.Listing Directories with glob
12.21.11.Get full path