Python递归删除.svn文件夹

来源:互联网 发布:c语言二叉查找树 编辑:程序博客网 时间:2024/05/01 19:40
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);

借鉴以上代码转自:

http://onlypython.group.javaeye.com/group/blog/53926

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.';

http://www.cnblogs.com/jaypei/archive/2008/05/08/1188764.html


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

#!/usr/bin/python    # -*- coding: utf8 -*-       import sys, os, statdef 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])

http://www.cnblogs.com/allenblogs/archive/2010/09/14/1826005.html


方法三:

#coding=utf-8import osimport shutilimport sysimport statdef 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 osfor (p,d,f) in os.walk("要删除的目录路径"):    if p.find('.svn')>0:        os.popen('rd /s /q %s'%p)

http://www.cnblogs.com/alexander-lee/archive/2010/02/23/1671905.html


原创粉丝点击