Recursively Deleting Files and Subdirectories : remove directory « File « Python Tutorial






import os

emptyDirs = []
path = "c:/"

def deleteFiles(dirList, dirPath):
    for file in dirList:
        print "Deleting " + file
        os.remove(dirPath + "/" + file)

def removeDirectory(dirEntry):
    print "Deleting files in " + dirEntry[0]
    deleteFiles(dirEntry[2], dirEntry[0])
    emptyDirs.insert(0, dirEntry[0])

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

for dir in emptyDirs:
    print "Removing " + dir
    os.rmdir(dir)








12.5.remove directory
12.5.1.Recursively Deleting Files and Subdirectories