Python删除文件夹下所有特定类型文件

写了个递归删除一个或多个文件夹下所有bmp图片的脚本:

import os

def del_bmp(root_dir="C:\temp"):
    """
    @Function: Delete all bmp image file in root_dir and its subdirectory 
    @root_dir: The target directory 
    """
    file_list = os.listdir(root_dir)
    for f in file_list:
        file_path = os.path.join(root_dir, f)
        if os.path.isfile(file_path):
            if f.endswith(".BMP") or f.endswith(".bmp"):
                os.remove(file_path)
                print " File removed! " + file_path
        elif os.path.isdir(file_path):
            del_bmp(file_path)


if __name__ == "__main__":
    root_dir = ["C:\\temp1\\", "C:\\temp2\\"]
    for target_dir in root_dir:
        del_bmp(target_dir)
        print "All BMP files deleted."

Comments !