python svn脚本需要我们不断的学习,在学习的过程中我们会遇到不少的问题,首先我们来看看在文件删除上的相关问题介绍。python svn脚本很好用,但是如果要删除svn文件夹的绑定是个很麻烦的事情,每个目录下都有.svn的文件夹,必须全部删掉才行。
手动删除费时费力,一般都写段脚本搞定,不过网上搜索出来的python脚本都太冗长了,一点也体现不出python的优势。如下,网上搜索到得代码:
代码
#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])
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
其中实现功能的核心代码写得太过冗长,其实很简单的4行代码就能达到目的,如下:
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)
- 1.
- 2.
- 3.
- 4.
以上就是对python svn脚本的详细介绍。
【编辑推荐】