Python递归删除.svn文件夹


  1. import os, stat;  
  2.   
  3.  root_dir = r'X:\XXX\XXX';  
  4.   
  5. def walk(path):  
  6.      for item in os.listdir(path):  
  7.          subpath = os.path.join(path, item);  
  8.          mode = os.stat(subpath)[stat.ST_MODE];  
  9.            
  10.          if stat.S_ISDIR(mode):  
  11.              if item==".svn":  
  12.                  print "Clean %s ..." % subpath;  
  13.                  print "%d deleted!" % purge(subpath);  
  14.              else:  
  15.                  walk(subpath);  
  16.   
  17. def purge(path):  
  18.      count = 0;  
  19.      for item in os.listdir(path):  
  20.          subpath = os.path.join(path, item);  
  21.          mode = os.stat(subpath)[stat.ST_MODE];  
  22.          if stat.S_ISDIR(mode):  
  23.              count += purge(subpath);  
  24.          else:  
  25.              os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE);  
  26.              os.unlink(subpath);  
  27.              count += 1;  
  28.      os.rmdir(path);  
  29.      count += 1;  
  30.      return count;  
  31.                         
  32.        
  33.   
  34. if __name__=='__main__':  
  35.      walk(root_dir);  

借鉴以上代码:

  1. import os, stat;  
  2.   
  3.  root_dir = r'X:\XX\XX';  
  4.   
  5.   
  6. def purge(path):  
  7.      count = 0;  
  8.      for item in os.listdir(path):  
  9.          subpath = os.path.join(path, item);  
  10.          mode = os.stat(subpath)[stat.ST_MODE];  
  11.          if stat.S_ISDIR(mode):  
  12.              count += purge(subpath);  
  13.          else:  
  14.              os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE);  
  15.              os.unlink(subpath);  
  16.              count += 1;  
  17.      os.rmdir(path);  
  18.      count += 1;  
  19.      return count;  
  20.   
  21.   
  22.   
  23. def callback(arg, directory, files):  
  24.      if os.path.split(directory)[1]=='.svn':  
  25.          print directory;  
  26.          #使用os.removedirs()删不掉   
  27.         print "Folder [%s](%d files) deleted." % (directory, purge(directory));  
  28.          print '--------------------';  
  29.   
  30.        
  31. if __name__=='__main__':  
  32.      print 'start';  
  33.      os.path.walk(root_dir, callback, 0);  
  34.      print 'complete.';  

第一种方法每次需要修改文件夹路径,进行了些修改

  1. #!/usr/bin/python       
  2. # -*- coding: utf8 -*-       
  3.      
  4. import sys, os, stat  
  5. def walk(path):  
  6.     for item in os.listdir(path):  
  7.         subpath=os.path.join(path, item)  
  8.         mode=os.stat(subpath)[stat.ST_MODE]  
  9.         if stat.S_ISDIR(mode):  
  10.             if item==".svn":  
  11.                 print "Cleaning %s ..." % subpath  
  12.                 print "%d deleted" % purge(subpath)  
  13.             else:  
  14.                 walk(subpath)      
  15.      
  16. def purge(path):  
  17.     count=0  
  18.     for item in os.listdir(path):  
  19.         subpath=os.path.join(path, item)  
  20.         mode=os.stat(subpath)[stat.ST_MODE]  
  21.         if stat.S_ISDIR(mode):  
  22.             count+=purge(subpath)  
  23.         else:  
  24.             os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE)  
  25.             os.unlink(subpath)  
  26.             count+=1  
  27.     os.rmdir(path)  
  28.     count+=1  
  29.     return count     
  30.      
  31. if len(sys.argv)!=2:  
  32.     print "Usage: python SVNClean.py path"  
  33.     sys.exit(1)      
  34.      
  35. walk(sys.argv[1])  

方法三:

  1. #coding=utf-8   
  2. import os  
  3. import shutil  
  4. import sys  
  5. import stat  
  6.   
  7. def deleteSubFile(svnpath):  
  8.     names = os.listdir(svnpath)  
  9.     for name in names:  
  10.           
  11.         fp = os.path.join( svnpath, name)  
  12.         if (os.path.isfile(fp)):  
  13.             os.chmod( fp, stat.S_IWRITE)  
  14.             os.remove(fp)  
  15.         else:  
  16.             deleteSubFile(fp)  
  17.               
  18.   
  19. def deleteSVN(parentPath = None, dir = None):  
  20.     if (dir != None and dir == '.svn'):  
  21.         deleteSubFile(os.path.join( parentPath, dir))  
  22.         shutil.rmtree(os.path.join( parentPath, dir), TrueFalse)  
  23.         print 'deleted ', os.path.join( parentPath, dir)  
  24.     else:  
  25.         if (dir != None):  
  26.             filePath = os.path.join( parentPath, dir)  
  27.         else:  
  28.             filePath = parentPath  
  29.         names = os.listdir(filePath)  
  30.         for name in names:  
  31.             fp = os.path.join( filePath, name)  
  32.             if (os.path.isdir(fp)):  
  33.                 deleteSVN(filePath, name)  
  34.   
  35.   
  36. if len(sys.argv) < 2:  
  37.     print 'Usage: python % <file path>' % os.path.basename(sys.argv[0])  
  38.     sys.exit(-1)  
  39.   
  40. if os.path.isfile(sys.argv[1]):  
  41.     print '请选择文件夹, 而不是文件'  
  42. else:  
  43.     deleteSVN(parentPath = sys.argv[1])  

4行python代码,删除svn文件夹

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

相关内容