python删除windows上指定文件夹中,所有文件名中包含指定字符串的文件

import os

folder_path = "E:\\myproject\\2.4.3"
search_str = ".bak"


for root, dirs, files in os.walk(folder_path):
    for file in files:
        file_path = os.path.join(root, file)
        if search_str in file:
            # print file_path
            os.remove(file_path)
            print "已删除文件:", file_path
posted @ 2023-04-20 10:15  vetra  阅读(168)  评论(0)    收藏  举报