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/
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的range()函数用法
- Windows下Python第三方.whl的安装
- Python类型——数字类型
- Python基础--快速改造:基础知识
- python入门介绍及使用
- Python基础--快速改造:基础知识
- Python中的并行工具
- Python学习笔记一--字符串的使用
- python变量命名规范
- python 编码问题
- Python导入自定义模块
- Python socket 解析
- Python爬虫爬数据写入到文件
- python反射机制
- Python集合(set)类型的操作
- Python爬取韩寒全部新浪博客
- Python爬虫模拟登录京东获取个人信息
- Pyenv/pyenv-virtualenv/jupyter的部署/安装/使用
- python的join用法
- python学习笔记-Day17 - web框架