Python 清理项目的目录

#-*- coding:utf-8 -*-

#=============================================
#
# author : yoshow
#
# filename : samples.clear.project.all.py
#
# summary : clear with project
#
#=============================================

import sys, os, stat

# 此脚本以删除.svn,bin,obj文件夹为例,删除某一文件雷同.
#
# Google一搜, 我发现网上有简单的方法,例如一下的shell命令.-_-
#
# window cmd
# for /r . %%a in (.) do @if exist "%%a.svn" rd /s /q "%%a.svn"
#
# linux shell
# find . -type f -iname ".svn" -exec rm -rf {} ;

def walk(path):
      """search files and directories."""
      for item in os.listdir(path):

            subpath=os.path.join(path, item)
            mode=os.stat(subpath)[stat.ST_MODE]
           

            if stat.S_ISDIR(mode):
                  view_child_dir = 1

                  # 此处填写你需要的删除的目录名称
                  list = ['.svn', 'debug', 'obj']

                  for dir_name in list :
                        if item.lower () == dir_name :
                              print "cleaning %s ..." % subpath
                              print "%d deleted" % purge(subpath)
                              view_child_dir = 0

                  #遍历子目录
                  if view_child_dir:
                  walk(subpath)

def purge(path):
      """purge mean clear some thing."""
     

      count=0

      for item in os.listdir(path):
            subpath=os.path.join(path, item)
            mode=os.stat(subpath)[stat.ST_MODE]

            if stat.S_ISDIR(mode):
                  count+=purge(subpath)
            else:
                  os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE)
                  os.unlink(subpath)
                  count+=1

            os.rmdir(path)
            count+=1

      return count

if __name__ == '__main__':

      #get current project's directory.

      path = os.path.normpath(os.getcwd())
      #print path
     

      walk(path)

posted @ 2008-01-27 20:07  Yoshow  阅读(471)  评论(1)    收藏  举报