百木园-与人分享,
就是让自己快乐。

【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\')


来源:https://www.cnblogs.com/yefuyi/p/15957355.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » 【python】删除包含指定文件名的文件夹

相关推荐

  • 暂无文章