码迷,mamicode.com
首页 > 编程语言 > 详细

python学习

时间:2015-02-03 13:07:15      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

下面这个例子是我写了个文件替换的小程序。替换所有.html文件里的图片的路径

import os
import re
t = re.compile(r‘\/?static\/|\/?media\/‘) #re.compile

template = ‘/home/laowangpython/‘
for root, dirs, files in os.walk(template):
    for f in files:
        if f.endswith(‘.html‘):
            tihuan = ‘http://www.cnpythoner.com/‘
            filename = ‘%s‘%(os.path.join(root,f))
            print filename
            f_a = file(filename,‘r‘)
            info = []
            for i in f_a:
                content =  t.sub(tihuan,i)
                info.append(content)
            finfo = "".join(info)
            b = file(filename,‘w‘)
            b.write(finfo)


原创文章:http://www.cnpythoner.com/post/17.html,转载请保留,谢谢!

python学习

标签:

原文地址:http://www.cnblogs.com/wcLT/p/4269460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!