[转] Python递归删除.svn文件夹

import os, stat;  
  
 root_dir = r'X:\XXX\XXX';  
  
def walk(path):  
     for item in os.listdir(path):  
         subpath = os.path.join(path, item);  
         mode = os.stat(subpath)[stat.ST_MODE];  
           
         if stat.S_ISDIR(mode):  
             if item==".svn":  
                 print "Clean %s ..." % subpath;  
                 print "%d deleted!" % purge(subpath);  
             else:  
                 walk(subpath);  
  
def purge(path):  
     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__':  
     walk(root_dir);  
借鉴以上代码:

import os, stat;  
  
 root_dir = r'X:\XX\XX';  
  
  
def purge(path):  
     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;  
  
  
  
def callback(arg, directory, files):  
     if os.path.split(directory)[1]=='.svn':  
         print directory;  
         #使用os.removedirs()删不掉   
        print "Folder [%s](%d files) deleted." % (directory, purge(directory));  
         print '--------------------';  
  
       
if __name__=='__main__':  
     print 'start';  
     os.path.walk(root_dir, callback, 0);  
     print 'complete.';  
第一种方法每次需要修改文件夹路径,进行了些修改

#!/usr/bin/python       
# -*- coding: utf8 -*-       
     
import sys, os, stat  
def walk(path):  
    for item in os.listdir(path):  
        subpath=os.path.join(path, item)  
        mode=os.stat(subpath)[stat.ST_MODE]  
        if stat.S_ISDIR(mode):  
            if item==".svn":  
                print "Cleaning %s ..." % subpath  
                print "%d deleted" % purge(subpath)  
            else:  
                walk(subpath)      
     
def purge(path):  
    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 len(sys.argv)!=2:  
    print "Usage: python SVNClean.py path"  
    sys.exit(1)      
     
walk(sys.argv[1])  
方法三:

#coding=utf-8   
import os  
import shutil  
import sys  
import stat  
  
def deleteSubFile(svnpath):  
    names = os.listdir(svnpath)  
    for name in names:  
          
        fp = os.path.join( svnpath, name)  
        if (os.path.isfile(fp)):  
            os.chmod( fp, stat.S_IWRITE)  
            os.remove(fp)  
        else:  
            deleteSubFile(fp)  
              
  
def deleteSVN(parentPath = None, dir = None):  
    if (dir != None and dir == '.svn'):  
        deleteSubFile(os.path.join( parentPath, dir))  
        shutil.rmtree(os.path.join( parentPath, dir), True, False)  
        print 'deleted ', os.path.join( parentPath, dir)  
    else:  
        if (dir != None):  
            filePath = os.path.join( parentPath, dir)  
        else:  
            filePath = parentPath  
        names = os.listdir(filePath)  
        for name in names:  
            fp = os.path.join( filePath, name)  
            if (os.path.isdir(fp)):  
                deleteSVN(filePath, name)  
  
  
if len(sys.argv) < 2:  
    print 'Usage: python % <file path>' % os.path.basename(sys.argv[0])  
    sys.exit(-1)  
  
if os.path.isfile(sys.argv[1]):  
    print '请选择文件夹, 而不是文件'  
else:  
    deleteSVN(parentPath = sys.argv[1])  
4行python代码,删除svn文件夹

import os  
for (p,d,f) in os.walk("要删除的目录路径"):  
    if p.find('.svn')>0:  
        os.popen('rd /s /q %s'%p)  

 

 

来源:http://www.linuxidc.com/Linux/2011-08/40716.htm

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值