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)
'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)

 

 

 

posted on 2018-04-17 09:07  石中玉smulngy  阅读(268)  评论(0编辑  收藏  举报

导航