python svn脚本如何进行文件删除

开发 后端
python svn脚本在使用的时候会遇到文件删除的问题,下面我们就来详细的学下相关的代码。希望大家有所收获。

python svn脚本需要我们不断的学习,在学习的过程中我们会遇到不少的问题,首先我们来看看在文件删除上的相关问题介绍。python svn脚本很好用,但是如果要删除svn文件夹的绑定是个很麻烦的事情,每个目录下都有.svn的文件夹,必须全部删掉才行。

手动删除费时费力,一般都写段脚本搞定,不过网上搜索出来的python脚本都太冗长了,一点也体现不出python的优势。如下,网上搜索到得代码:

代码
 

  1. #coding=utf-8  
  2. import os  
  3. import shutil  
  4. import sys  
  5. import stat  
  6. def deleteSubFile(svnpath):  
  7. names = os.listdir(svnpath)  
  8. for name in names:  
  9. fp = os.path.join( svnpath, name)  
  10. if (os.path.isfile(fp)):  
  11. os.chmod( fp, stat.S_IWRITE)  
  12. os.remove(fp)  
  13. else:  
  14. deleteSubFile(fp)  
  15. def deleteSVN(parentPath = Nonedir = None):  
  16. if (dir != None and dir == '.svn'):  
  17. deleteSubFile(os.path.join( parentPath, dir))  
  18. shutil.rmtree(os.path.join( parentPath, dir), True, False)  
  19. print 'deleted ', os.path.join( parentPath, dir)  
  20. else:  
  21. if (dir != None):  
  22. filePath = os.path.join( parentPath, dir)  
  23. else:  
  24. filePath = parentPath 
  25. names = os.listdir(filePath)  
  26. for name in names:  
  27. fp = os.path.join( filePath, name)  
  28. if (os.path.isdir(fp)):  
  29. deleteSVN(filePath, name)  
  30. if len(sys.argv) < 2: 
  31. print 'Usage: python % <file path>' % os.path.basename(sys.argv[0])  
  32. sys.exit(-1)  
  33. if os.path.isfile(sys.argv[1]):  
  34. print '请选择文件夹, 而不是文件'  
  35. else:  
  36. deleteSVN(parentPath = sys.argv[1]) 

其中实现功能的核心代码写得太过冗长,其实很简单的4行代码就能达到目的,如下:

 

  1. 1 import os  
  2. 2 for (p,d,f) in os.walk("要删除的目录路径"):  
  3. 3 if p.find('.svn')>0:  
  4. 4 os.popen('rd /s /q %s'%p) 

以上就是对python svn脚本的详细介绍。

【编辑推荐】

  1. Python正则表达式十种相关的匹配方法
  2. Python字符串替换如何才能进行字符的拆分
  3. Python文本乱码发生时的解决方案
  4. Python编程语言总体性能优点评测
  5. Python编程语言具有相当高的适应能力
责任编辑:张浩 来源: IT168
相关推荐

2011-09-01 18:54:29

WifiGoodReader

2010-06-02 14:16:18

SVN版本控制

2010-06-02 10:10:26

SVN源码管理

2010-05-28 12:55:23

2010-06-01 14:35:16

SVN仓库导出

2010-05-27 16:18:58

SVN设置

2010-07-19 11:10:58

SQL Server

2010-02-03 13:55:51

Python 代码

2010-02-22 16:05:40

Python配置

2010-02-01 10:21:36

Python编码转换

2010-06-02 18:51:35

搭建SVN服务器

2010-05-25 18:32:22

apache+svn+

2010-02-01 18:20:17

Python 多重继承

2010-03-11 17:38:20

Python中文

2010-02-02 16:47:12

Python主线程

2010-03-15 15:11:50

Python列表

2010-02-01 14:48:43

2010-02-01 17:33:24

Python主线程

2016-12-14 09:32:49

FileChanne文件复制

2010-02-02 13:04:03

C++头文件
点赞
收藏

51CTO技术栈公众号