Recursively Deleting Files and Subdirectories : Remove directory : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Remove directory »

 

Recursively Deleting Files and Subdirectories


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)



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Remove directory