Python递归删除.svn文件夹
2008-05-08 18:01 Jaypei 阅读(1071) 评论(0) 编辑 收藏 举报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);
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.';
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.';