码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-17 11:35:54      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:3.3   创建   uid   size   文件权限   list   open   如何   UI   

技术分享图片

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)
0100764
>>> 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开头或结尾

标签:3.3   创建   uid   size   文件权限   list   open   如何   UI   

原文地址:https://www.cnblogs.com/smulngy/p/8861720.html

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