Skip to content Skip to sidebar Skip to footer

Delete A Current Directory Based On If Condition

Following code counts number of image in each sub directory. how to delete a sub directory if images in sub-directory are more than 2. n13 is main directory=> which have 300 sub

Solution 1:

You can use shutil.rmtree() to delete a folder with its sub-directories and files.

import os
import shutil

path='C:/n13/'

def count_em(path):
    x = 0
    files_count = 0
    for root, dirs, files in os.walk(path):
        files_count = (len(files))
        if files_count >= 2:
            shutil.rmtree(root)
        x = x + 1
        print("Images:", files_count, "Directory:", x)
    return files_count


count_em(path)

Post a Comment for "Delete A Current Directory Based On If Condition"