Python脚本删除特定文件夹
# 在下面的代码中,
# 将 path 替换为你想要删除的文件夹的路径,
# 将 string 替换为你想要删除的文件夹名称中包含的字符串。
# 这个代码使用了 os 和 shutil 模块。os.walk() 函数可以遍历指定路径下的所有文件和文件夹,
# topdown=False 表示从底层文件夹开始遍历。然后,我们可以使用
# if 语句来检查文件或文件夹名称是否包含特定字符串,
# 如果是,则使用 os.remove() 或 shutil.rmtree() 函数删除它们
import os
import shutil
def delete_folder(path, string):
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
if string in name:
os.remove(os.path.join(root, name))
for name in dirs:
if string in name:
shutil.rmtree(os.path.join(root, name))
path = r'D:\path'
string = r'Binaries'
string1 = r'Intermediate'
delete_folder(path, string)
delete_folder(path, string1)