4-2如何判断字符串a是否以字符串b开头或结尾

1、相关介绍
1.1修改文件权限和查看文件权限
在windows平台实验时 os.chmod()无法将文件权限修改为可执行,暂不深究如何实现。在linux平台进行测试。
(1)创建三个文件
python@ubuntu:/tmp/vmware-python$ ls
vmware-apploader-5673.log
python@ubuntu:/tmp/vmware-python$ touch a.py b.sh c.txt
python@ubuntu:/tmp/vmware-python$ ls -l
总用量 8
-rw-rw-r-- 1 python python 0 10月 26 17:26 a.py
-rw-rw-r-- 1 python python 0 10月 26 17:26 b.sh
-rw-rw-r-- 1 python python 0 10月 26 17:26 c.txt
-rw-r----- 1 python python 4532 10月 26 17:22 vmware-apploader-5673.log
(2)在linux shell界面进入python shell
python@ubuntu:/tmp/vmware-python$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(3)查看文件权限
>>> import os,stat
>>> os.stat('a.py')
posix.stat_result(st_mode=33204, st_ino=786703, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1509009983, st_mtime=1509009983, st_ctime=1509009983)
>>> oct(os.stat('a.py').st_mode)
'0100664' #其中 “664”就是权限代码
(4)修改文件权限
>>> stat.S_IXUSR #执行权限的掩码
64
>>> os.chmod('a.py',os.stat('a.py').st_mode |stat.S_IXUSR)
>>> oct(os.stat('a.py').st_mode)
''
>>> exit()
再次查看文件权限
python@ubuntu:/tmp/vmware-python$ ls
a.py b.sh c.txt vmware-apploader-5673.log
python@ubuntu:/tmp/vmware-python$ ls -l
总用量 8
-rwxrw-r-- 1 python python 0 10月 26 17:26 a.py
-rw-rw-r-- 1 python python 0 10月 26 17:26 b.sh
-rw-rw-r-- 1 python python 0 10月 26 17:26 c.txt
-rw-r----- 1 python python 4532 10月 26 17:22 vmware-apploader-5673.log
1.2用符串的endswith()的方法
>>> s = 'a.py'
>>> s.endswith('.py')
True
endswith()可以接受元组参数,只要满足元组的一项,即为真。(参数只能是元组不能是列表)
>>> s.endswith(('.py','.sh'))
True
>>> s.endswith(('.sh','.txt'))
False
2、实现方法
>>> import os,stat
>>> [x for x in os.listdir('.') if x.endswith(('.sh','.py'))]
['a.py', 'b.sh'] >>> lFile = [x for x in os.listdir('.') if x.endswith(('.sh','.py'))]
>>> for x in lFile:
... os.chmod(x,os.stat(x).st_mode | stat.S_IXUSR)
...
>>> os.listdir('.')
['vmware-apploader-5673.log', 'c.txt', 'a.py', 'b.sh']
>>> exit()
查看文件权限:
python@ubuntu:/tmp/vmware-python$ ls -l
总用量 8
-rwxrw-r-- 1 python python 0 10月 26 17:26 a.py
-rwxrw-r-- 1 python python 0 10月 26 17:26 b.sh
-rw-rw-r-- 1 python python 0 10月 26 17:26 c.txt
-rw-r----- 1 python python 4532 10月 26 17:22 vmware-apploader-5673.log
>>> import os
>>> [x for x in os.listdir('.') if x.endswith(('.sh','.py'))]
['a.py', 'b.sh']
3、扩展知识
3.1 os.stat()
>>> help(os.stat)
Help on built-in function stat in module nt: stat(...)
stat(path) -> stat result Perform a stat system call on the given path.
help(os.stat)
获取文件属性:os.stat(file),如本例中
>>> os.stat('a.py')
posix.stat_result(st_mode=33204, st_ino=786703, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1509009983, st_mtime=1509009983, st_ctime=1509009983)
3.2 str.startswith()
>>> help(str.startswith)
Help on method_descriptor: startswith(...)
S.startswith(prefix[, start[, end]]) -> bool Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
help(str.startswith)
3.3 str.endswith()
>>> help(str.endswith)
Help on method_descriptor: endswith(...)
S.endswith(suffix[, start[, end]]) -> bool Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
help(str.endswith)
4-2如何判断字符串a是否以字符串b开头或结尾的更多相关文章
- C#算法之判断一个字符串是否是对称字符串
记得曾经一次面试时,面试官给我电脑,让我现场写个算法,判断一个字符串是不是对称字符串.我当时用了几分钟写了一个很简单的代码. 这里说的对称字符串是指字符串的左边和右边字符顺序相反,如"abb ...
- String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
// 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...
- php 判断字符串在另一个字符串中位置
$email='user@example.com'; //定义字符串$result=strstr($email,'@'); //返回子字符串echo $result; / ...
- Mysql查找如何判断字段是否包含某个字符串
Mysql查找如何判断字段是否包含某个字符串 有这样一个需求,在Mysql数据库字符串字段(权限)中,用户有多个不同的邮箱,分别被‘,’分开,现在要取出某个邮箱的所有成员列表. 假设有个表: ...
- 【功能代码】---3 JS判断字符串是否包含某个字符串
JS判断字符串是否包含某个字符串 var str ="abc"; if(str.indexOf("bc")>-1){ alert('str中包含bc字符串 ...
- go golang 判断base64数据 获取随机字符串 截取字符串
go golang 判断base64数据 获取随机字符串 截取字符串 先少写点,占个坑,以后接着加. 1,获取指定长度随机字符串 func RandomDigits(length int) strin ...
- python_如何判断字符串a以某个字符串开头或结尾?
案例: 某文件系统目录下有一系列文件: 1.c 2.py 3.java 4.sh 5.cpp ...... 编写一个程序,给其中所有的.sh文件和.py文件加上可执行权限 如何解决这个问题? 1. 先 ...
- mysql判断表里面一个逗号分隔的字符串是否包含单个字符串、查询结果用逗号分隔
1.mysql判断表里面一个逗号分隔的字符串是否包含单个字符串 : FIND_IN_SET select * from tablename where FIND_IN_SET(传的参数,匹配字段) 例 ...
- java算法:统计数字-将数字转换成字符串,然后使用字符串String.valueOf()方法进行判断
题目: 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 样例 1: 输入: k = 1, n = 1 输出: 1 解释: 在 [0, 1] 中,我们发现 1 出现了 ...
- js判断字符串str是否包含字符串substr
js判断字符串str是否包含字符串substr: function addUser(id,realName){ var userids = $("#userids").val(); ...
随机推荐
- 13. ClustrixDB 基于时间点恢复
在不太可能发生灾难的情况下,可以在特定数据库.表或整个集群上执行ClustrixDB集群的某个时间点恢复.应该非常小心地处理这一问题. 先决条件 在你可以使用时间点恢复之前,你的集群应该有几个先决条件 ...
- [算法]概率与期望DP
前言 前两节主要针对题目分析,没时间的珂以跳过. 初步 首先举一道简单.经典的好题: [Lightoj1038]Race to 1 Again 懒得单独写,安利一下DennyQi同学的博客:https ...
- android 文件保存到应用和sd卡中
<span style="font-size:18px;">1.权限添加 <uses-permission android:name="android. ...
- python3学习笔记(二):Python初识
一.算法 在开始认真地编程之前,首先来解释下什么是计算机程序设计.简单地说,它就是告诉计算机要做什么.计算机可以做很多事情,但是它不会自己思考,需要我们告诉它具体细节,并且使用计算机能够理解的语言把算 ...
- Tree and Permutation
Tree and Permutation 给出一个1,2,3...N的排列,显然全部共有N!种排列,每种排列的数字代表树上的一个结点,设Pi是其中第i种排列的相邻数字表示的结点的距离之和,让我们求su ...
- EMC存储同时分配空间到两台LINUX服务器路径不一致导致双机盘符大小不一致
操作系统:Centos linux6.6 当我们从EMC存储上划分空间同时分配给两台或者多台服务器上时,有的时候会出现在服务器上所生成的磁盘路径是不一致的,这样就会导致盘符名称不一致或者是盘符对应的大 ...
- Vue.use源码分析(转)+如何封装一个组件
封装一个组件:https://www.jianshu.com/p/89a05706917a 我想有过vue开发经验的,对于vue.use并不陌生.当使用vue-resource或vue-router等 ...
- JAVA单例模式的实现伪代码
什么是单例?:其定义是单例对象的类只能允许一个实例存在 单例的实现基本原理:1.将该类的构造方法定义为私有访问,不对外暴露从而使其他类不能实例化该类对象,只能通过该类的静态方法得到该类的唯一实例 2. ...
- 实验报告二&第四周学习总结
一.实验目的: (1) 掌握类的定义,熟悉属性.构造函数.方法的调用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性: (3 ...
- rosbag record and play
话题录制: 录制所有发布出来的话题,此时默认将话题保存在一个以当时时间戳命名的文件夹中: $ rosbag record -a1 录制指定话题: $ rosbag record /topic1 ...