zoukankan      html  css  js  c++  java
  • 删除指定目录下包含指定名称的文件夹(递归) python

    在整理mfc程序的时候, 为了清理项目资源, 经常需要删除项目目录或其子目录下(递归)的Debug, Release, x64, .vs等文件目录, 如果有很多项目, 手工清理很是麻烦, bat不太熟悉, 没找到合适的脚本, 自己用python写了一个清理的小脚本,

    使用用例:

    python demo.py CleanDir Debug, Release, x64, .vs

     1 import os
     2 import sys
     3 
     4 def DelDirAndSubDirByName(rootPath, dirNameList):
     5     subPath = ''
     6     for item in os.listdir(rootPath):
     7         subPath = rootPath + '\' + item
     8         if os.path.isdir(subPath):
     9             if item in dirNameList:
    10                 DelAllFile(subPath)
    11                 print('del dir: %s' %(subPath))
    12             else:
    13                 DelDirAndSubDirByName(subPath, dirNameList)
    14 
    15 def DelAllFile(delPath):
    16     if os.path.isfile(delPath):
    17         os.remove(delPath)
    18     else:
    19         for item in os.listdir(delPath):
    20             DelAllFile(delPath + '\' + item)
    21         os.rmdir(delPath)  
    22 
    23 if __name__ == '__main__':
    24     if  len(sys.argv) < 2 or 
    25         sys.argv[1] == '/?' or 
    26         sys.argv[1] == '-h' or 
    27         sys.argv[1] == '--help':
    28         print('usage: exe.py delPath delDirName1 delDirName2 ...')
    29         exit(0)
    30 
    31     delPath = os.path.abspath(sys.argv[1])
    32     print(delPath)
    33     if os.path.exists(delPath) != True:
    34         print('%s is not exist' %(delPath))
    35         exit(1)
    36 
    37     if os.path.isdir(delPath) != True:
    38         print('%s is not dir' %(delPath))
    39         exit(2)
    40 
    41     dirNameList = sys.argv[2:]
    42     DelDirAndSubDirByName(delPath, dirNameList)
  • 相关阅读:
    python 自动化之路 day 10 协程、异步IO、队列、缓存
    MySQ binlog三种模式
    文件存储之-内存文件系统tmpfs
    Linux 系统结构详解
    服务端高性能数据库优化演变细节案例
    滴滴研发笔记题,亮灯问题
    linux screen 命令详解
    Linux之在CentOS上一次艰难的木马查杀过程
    python 自动化之路 day 09 进程、线程、协程篇
    redis
  • 原文地址:https://www.cnblogs.com/endenvor/p/14049161.html
Copyright © 2011-2022 走看看