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/


你可能感兴趣的:(python 修改指定路径的csd文件)