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

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

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 = Nonedir = 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脚本的详细介绍。

【编辑推荐】

  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

2024-11-20 10:00:00

Python文件读写

2010-06-01 14:35:16

SVN仓库导出

2010-05-27 16:18:58

SVN设置

2010-07-19 11:10:58

SQL Server

2010-02-22 16:05:40

Python配置

2010-02-01 10:21:36

Python编码转换

2024-07-08 08:38:37

Python游戏开发

2010-02-03 13:55:51

Python 代码

2010-06-02 18:51:35

搭建SVN服务器

2010-05-25 18:32:22

apache+svn+

2016-12-14 09:32:49

FileChanne文件复制

2010-02-02 13:04:03

C++头文件

2010-06-02 11:39:09

tomcat和svn安

2010-02-01 17:33:24

Python主线程

2010-02-01 14:48:43

2010-02-02 16:47:12

Python主线程
点赞
收藏

51CTO技术栈公众号