如果你要用python匹配字符串的开头或末尾是否包含一个字符串,就可以用startswith,和endswith
比如:content = 'ilovepython'
如果字符串content以ilove开始,返回True,否则返回False
content.startswith("ilove")
返回true
content.startswith("sss")
返回false

如果字符串content以python结尾,返回True,否则返回False
content.endswith('python')
返回true
content.endswith("sss")
返回false

下面这个例子是我写了个文件替换的小程序。替换所有.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)

最新文章

  1. 分页型Memory LCD显存管理与emWin移植
  2. js的url解析函数封装
  3. WaterfallFlowLayout瀑布流用重写UICollectionViewFlowLayout类实现
  4. DataGridView的Cell事件的先后触发顺序
  5. Java--如何使用sun.misc.Unsafe完成compareAndSwapObject原子操作
  6. 游戏控制杆OUYA游戏开发快速入门教程
  7. DevExpress GridView 自定义搜索按钮改为中文内容
  8. OO之策略模式
  9. ##DAY10 UITableView基础
  10. HDU 5352 MZL's City
  11. 前端数据存储方案集合(cookie localStorage等)以及详解 (一)
  12. Python之测试webservice接口
  13. timestamp时间戳的应用(微信小程序开发也一样)
  14. http协议重点
  15. Linux必备操作vim
  16. Red Hat Enterprise Linux 5 64-bit chinese language support config steps
  17. Spring Boot 2.0系列文章(七):SpringApplication 深入探索
  18. php 识别二维码(转载)
  19. git add * 提示warning: LF will be replaced by CRLF in 解决办法
  20. Go 1.11 Module 介绍

热门文章

  1. myeclipse 10 j安装了JDK1.7,java编译器无法选择到1.7的问题
  2. BZOJ4311:向量
  3. Bluetooth Profile for iPhone from the functional perspectives
  4. Anton and School - 2 (组合数学)
  5. servlet中service() doGet() doPost() 方法
  6. IDEA运行debug为灰色无法运行
  7. JMeter报错 ERROR o.a.j.t.JMeterThread: Test failed!
  8. aodh M版本新特性 - Remove eventlet from Aodh in favour of threaded approach
  9. review35
  10. hzau 1205 Sequence Number(二分)