Python递归删除.svn文件夹
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)
评论暂时关闭