项目中用到python判断一个字符串是否以某个字符串结尾,比如,筛选一个目录下所有以.mp4结尾的文件。

>>> item = "demo.mp4"
>>> item.endswith('.mp4')
True
>>> item.endswith('.json')
False
>>> item.startswith('demo')
True
>>> item.startswith('index')
False
>>>

例子:

将/tmp目录下所有的mp4文件转移到/home目录下

import shutil
import os
file_list = os.listdir('/tmp')
for item in file_list:
if item.endswith('.mp4'):
shutil.move('/tmp/%s' %item, '/home/%s' %item)

【python】判断字符串以什么开头或结尾的更多相关文章

  1. Python判断字符串是否xx开始或结尾

    判断是否xx开始 使用startswith 示例代码: String = "12345 上山打老虎" if str(String).startswith('1'): #判断Stri ...

  2. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  3. js正则表达式,判断字符串是否以数字组结尾,并取出结尾的数字

    js正则表达式,判断字符串是否以数字组结尾,并取出结尾的数字 <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  4. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  5. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  6. Python判断字符串编码以及编码的转换

    转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...

  7. python判断字符串中是否包含子字符串

    python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1:     print('存在') else:     print('不存在' ...

  8. python判断字符串是否是json格式方法分享

    python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例:   ...

  9. Python判断字符串是否为字母或者数字

    严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...

随机推荐

  1. [ci]安装配置jenkins及其插件

    后面尝试ms模式部署多台jenkins 安装jenkins:(hudson是jenkins的商业版) cd /etc/yum.repos.d/ wget http://pkg.jenkins.io/r ...

  2. 初识WatiN

    WatiN —— Web Application Testing In .Net 为什么会有WatiN? 给用户提供一个.Net平台下,将Web测试自动化的便捷途径. 如何通过WatiN来进行自动化测 ...

  3. android studio - 隐藏编辑器上面的竖线

    android studio 的代码编辑器上面默认有一条竖线不知道是干什么用的,很难看.可以用下列方法进行隐藏. 取消选中这个复选框即可.

  4. Window安装Anaconda后,conda不是内部或者外部命令

    今天在安装Theano的时候,需要看一下,anaconda已经安装了哪些包.使用命令如下,在控制台,cmd回车输入即可: conda list 但是,显示出错,“conda不是内部或者外部命令”,第一 ...

  5. java继承机制

    1 继承  关键字:extends   java没有多重继承 实例 父类: package unit4; public class Base { public int publicVarofBase= ...

  6. quantz入门和使用流程(转载)

    1.下载地址:http://quartz-scheduler.org/downloads/catalog http://quartz-scheduler.org/downloads/destinati ...

  7. selenium + js 处理窗口

    1.隐藏页面的广告窗口 document.getElementById("top_left").style.display="none"; 2.隐藏控件点击 d ...

  8. web开发之php--- mvc 模式

    http://www.cnblogs.com/archy_yu/p/4229929.html

  9. 移动web开发经验总结(转)

    1.<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-sca ...

  10. python3----split and join

    s = "I am fine" s = s.split(" ") print(s) print("%".join(s)) results: ...