编程语言
首页 > 编程语言> > 【python】删除包含指定文件名的文件夹

【python】删除包含指定文件名的文件夹

作者:互联网

代码

import os
import os.path as pth
import shutil


def rm_empty_dir(path: str, filename_tag: str):
    all_files = os.listdir(path)
    count = 0
    for file in all_files:
        dirname = pth.join(path, file)
        if pth.isdir(dirname):
            sub_dir_files = os.listdir(dirname)
            for sub_file in sub_dir_files: 
              if filename_tag in sub_file:
                  # 当前文件夹找到了该文件名中包含filename_tag
                  print(dirname, 'delete sucessfully!')
                  # 递归删除当前目录
                  shutil.rmtree(dirname)
                  count = count + 1
    print('Total {} files deleted'.format(count))


if __name__ == '__main__':
    path = r'E:\runs\temp'
    rm_empty_dir(path, 'test')

标签:files,__,sub,文件名,python,文件夹,file,path,dirname
来源: https://www.cnblogs.com/yefuyi/p/15957355.html