python 修改指定路径的csd文件



import os,shutil
import re
def GetFileFromThisRootDir(dir,ext = None):
    allfiles = []
    needExtFilter = (ext != None)
    for root,dirs,files in os.walk(dir):
        for filespath in files:
            filepath = os.path.join(root, filespath)
            extension = os.path.splitext(filepath)[1][1:]
            if needExtFilter and extension in ext:
                allfiles.append(filepath)
            elif not needExtFilter:
                allfiles.append(filepath)
    return allfiles

def GetPathFromThisLineWithStr(line, pathname):
 allpaths = []
 strstart = line.find(pathname) + len(pathname) + 1
 strend = strstart
 strtag = strstart
 while cmp(line[strend], "\""):
  if 0==cmp(line[strend], "/"):
   strtag = strend + 1
  strend += 1
 allpaths.append(line[strstart:strend])
 allpaths.append(line[strtag:strend])
 return allpaths

def modifyStrPaths(tfile, pathname):
    lines=open(tfile,'r').readlines()
    flen=len(lines)-1
    for i in range(flen):
        if pathname in lines[i]:
            paths = GetPathFromThisLineWithStr(lines[i], pathname)
            if not 0==cmp(paths[0], paths[1]): 
             lines[i]=lines[i].replace(paths[0],paths[1])
             print i, paths[0], paths[1]
        open(tfile,'w').writelines(lines)

fileArr = GetFileFromThisRootDir("./","csd")
filelen=len(fileArr)
for fn in range(filelen):
 print fileArr[fn]
 modifyStrPaths(fileArr[fn]," Path=")
print filelen, "files finish"

os.system("pause")
#raw_input( )


http://l90z11.blog.163.com/blog/static/187389042201312153318389/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值