示例:Python删除 .svn 文件(Window7测试通过)
#! /usr/bash/python #! encoding:utf-8 import os import os.path import stat import shutil class DelDir: ''' 删除指定根目录下特定文件夹 ''' def __init__(self, root, dirname): self.root = root self.dirname = dirname def run(self): for r, dirs, files in os.walk(self.root): if self.dirname in dirs: srcDir = os.path.join(r, self.dirname) #更改权限(win7会出现权限问题) os.chmod(srcDir, stat.S_IREAD | stat.S_IWRITE) result = shutil.rmtree(srcDir, False, self.__handler) print "%s" %(srcDir) def __handler(self, function, path, excinfo): ''' 删除出错处理 ''' #更改权限(win7会出现权限问题) os.chmod(path, stat.S_IREAD | stat.S_IWRITE) function(path) print "[Handler] ==> Path:%s \n\tHandler the Error: %s" %(path, excinfo) if __name__ == '__main__': rootdir = r"E:\workspace_new\static" # 需要处理的文件夹 # rootdir = unicode(rootdir, "utf8") dirname = ".svn" # 删除的文件夹 c = DelDir(rootdir, dirname) c.run()