Python 清除SVN文件

[size=medium]
初学Python,为熟悉文件相关操作,写了一个简单且粗糙的清除项目下svn文件功能。
[/size]

import os
import time
import stat

def clearSVN(destPath):
if os.path.isdir(destPath) == False :
return;
else :
destFileName = os.path.basename(destPath)
print 'current dir name is %s' %destFileName
if str(destFileName).find('svn') == -1 :
print '%s is not svn file' %destFileName
for fDir in os.listdir(destPath) :
clearSVN(os.path.join(destPath, fDir))
else :
print '%s is svn file' %destFileName
os.chmod(destPath, stat.S_IMODE(os.stat(destPath)[stat.ST_MODE]) | stat.S_IREAD|stat.S_IWRITE)
for root, dirs, files in os.walk(destPath, topdown=False):
for fileN in files:
try :
f = os.path.join(root, fileN);
os.chmod(f, stat.S_IMODE(os.stat(f)[stat.ST_MODE]) | stat.S_IREAD|stat.S_IWRITE)
os.remove(f)
except OSError as e:
print f + 'remove error' + e.message
finally :
if os.path.exists(f) :
os.system('del ' + f + ' /f /q')
for dirN in dirs:
print 'dirName is %s' %dirN
try :
print 'root is %s' %root
os.chmod(root, stat.S_IMODE(os.stat(root)[stat.ST_MODE]) | stat.S_IREAD|stat.S_IWRITE)
os.rmdir(root)
except OSError as e:
print root + 'remove error' + e.message
finally :
if os.path.exists(root) :
os.system('del ' + root + ' /f /q')

def search(destPath, destFile):
if os.path.isdir(destPath) == False or os.path.isfile(destFile) == False :
return
for destF in os.listdir(destPath):
if os.path.isfile(destF) and destF == os.path.basename(destFile) :
return os.path.join(destPath, destF)
elif os.path.isdir(destF) :
search(os.path.join(destPath, destF), destFile)
else :
print 'can not find file %s' %destFile
return None

def iteratorHandle(srcPath, destPath, isNew = False):
if os.path.isfile(srcPath) :
destFile = search(destPath, srcPath)
if destFile == None :
if isNew == False :
if os.path.isfile(srcPath) :
try :
os.chmod(srcPath, stat.S_IMODE(os.stat(srcPath)[stat.ST_MODE]) | stat.S_IREAD|stat.S_IWRITE)
os.remove(srcPath)
finally :
os.system('del ' + srcPath + ' /F')
else :
print 'new file to add'
else :
newFileMT = time.localtime(os.stat(destFile).st_mtime)
oldFileMT = time.localtime(os.stat(srcPath).st_mtime)
if time.mktime(newFileMT) - time.mktime(oldFileMT) > 0 :
oldFileDir = str(srcPath)[0:str(srcPath).rfind(os.path.sep)]
oldFileName = os.path.basename(srcPath)
oldFilenewName = str(srcPath)[0 : str(srcPath).rfind('.')] + '_backup' + str(srcPath)[str(srcPath).rfind('.') : ]
os.rename(srcPath, os.path.join(oldFileDir, oldFilenewName))
open(os.path.join(oldFileDir, oldFileName), "wb").write(open(destFile, "rb").read())
elif os.path.isdir(srcPath) :
for destFile in os.listdir(srcPath) :
iteratorHandle(os.path.join(srcPath, destFile))
else :
print 'iterator handle error'

def handle(srcPath, destPath):
if os.path.isdir(srcPath) == False or os.path.isdir(destPath):
return
clearSVN(srcPath)
clearSVN(destPath)

fileDir = 'C:\\Users\\Administrator\\Desktop\\project'
clearSVN(fileDir)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值