python startswith与endswith
如果你要用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)
最新文章
- 分页型Memory LCD显存管理与emWin移植
- js的url解析函数封装
- WaterfallFlowLayout瀑布流用重写UICollectionViewFlowLayout类实现
- DataGridView的Cell事件的先后触发顺序
- Java--如何使用sun.misc.Unsafe完成compareAndSwapObject原子操作
- 游戏控制杆OUYA游戏开发快速入门教程
- DevExpress GridView 自定义搜索按钮改为中文内容
- OO之策略模式
- ##DAY10 UITableView基础
- HDU 5352 MZL's City
- 前端数据存储方案集合(cookie localStorage等)以及详解 (一)
- Python之测试webservice接口
- timestamp时间戳的应用(微信小程序开发也一样)
- http协议重点
- Linux必备操作vim
- Red Hat Enterprise Linux 5 64-bit chinese language support config steps
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
- php 识别二维码(转载)
- git add * 提示warning: LF will be replaced by CRLF in 解决办法
- Go 1.11 Module 介绍
热门文章
- myeclipse 10 j安装了JDK1.7,java编译器无法选择到1.7的问题
- BZOJ4311:向量
- Bluetooth Profile for iPhone from the functional perspectives
- Anton and School - 2 (组合数学)
- servlet中service() doGet() doPost() 方法
- IDEA运行debug为灰色无法运行
- JMeter报错 ERROR o.a.j.t.JMeterThread: Test failed!
- aodh M版本新特性 - Remove eventlet from Aodh in favour of threaded approach
- review35
- hzau 1205 Sequence Number(二分)