您的位置:首页 > 编程语言 > Python开发

python 修改指定路径的csd文件

2016-03-08 00:29 681 查看

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: