svn很好用,但是如果要刪除svn檔案夾的綁定是個很麻煩的事情,每個目錄下都有.svn的檔案夾,必須全部刪掉才行。手動刪除費時費力,一般都寫段指令碼搞定,不過網上搜尋出來的python指令碼都太冗長了,一點也體現不出python的優勢。如下,網上搜尋到得代碼:
代碼 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), True, False)
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行代碼就能達到目的,如下:
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)