python3_如何判断字符串什么开头或者以某个字符串开头或结尾?

Python
In [15]: import os In [16]: os.listdir('.') Out[16]: ['oldurl2.txt', 'd1.py', 'kkk.mp4', 'oldurl3.txt', 'oldurl1.txt', 'd5.py', 'oldurl0.txt', 'oldurl4.txt', '.DS_Store', 'd4.py', 'oldurl5.txt', 'oldurl7.txt', 'oldurl6.txt', 'data1.py', '.rand_num.py.swp', 'get_artlist_stattuse.py', 'd7.py', 'oldurl8.txt', 'kk.html', 'k.mp4', 'd3.py', 'de.py', 'd2.py', 'foo.py', 'bd.py', 'd6.py', '.idea', 'art.data'] In [19]: [x for x in os.listdir('.') if x.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/endswith" title="View all posts in endswith" target="_blank">endswith</a></span>(('.py','.txt'))] Out[19]: ['oldurl2.txt', 'd1.py', 'oldurl3.txt', 'd6.py'] """获取文件的权限""" In [20]: import os In [21]: os.stat('bd.py') Out[21]: os.stat_result(st_mode=33188, st_ino=8596213658, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1377, st_atime=1510551606, st_mtime=1510551605, st_ctime=1510551605) In [22]: os.stat('bd.py').st_mode Out[22]: 33188 In [23]: oct(os.stat('bd.py').st_mode) Out[23]: '0o100644' import os import stat # 获取文件的状态 # 获得当前文件下目录 file_l = os.listdir('.') # <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/startswith" title="View all posts in startswith" target="_blank">startswith</a></span>中参数必须是元组形式,只有满足一个条件,返回True target_file = [name for name in file_l if name.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/startswith" title="View all posts in startswith" target="_blank">startswith</a></span>(('.sh', '.py'))] for file in target_file: # 给满足条件的文件所有者赋予执行权限 os.chmod(file, os.stat(file).st_mode | stat.S_IXUSR) # os.stat(file).st_mode | stat.S_IXUSR 获取执行的权限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
In [ 15 ] : import os
 
In [ 16 ] : os . listdir ( '.' )
Out [ 16 ] :
[ 'oldurl2.txt' ,
'd1.py' ,
'kkk.mp4' ,
'oldurl3.txt' ,
'oldurl1.txt' ,
'd5.py' ,
'oldurl0.txt' ,
'oldurl4.txt' ,
'.DS_Store' ,
'd4.py' ,
'oldurl5.txt' ,
'oldurl7.txt' ,
'oldurl6.txt' ,
'data1.py' ,
'.rand_num.py.swp' ,
'get_artlist_stattuse.py' ,
'd7.py' ,
'oldurl8.txt' ,
'kk.html' ,
'k.mp4' ,
'd3.py' ,
'de.py' ,
'd2.py' ,
'foo.py' ,
'bd.py' ,
'd6.py' ,
'.idea' ,
'art.data' ]
In [ 19 ] : [ x for x in os . listdir ( '.' ) if x . endswith ( ( '.py' , '.txt' ) ) ]
Out [ 19 ] :
[ 'oldurl2.txt' ,
'd1.py' ,
'oldurl3.txt' ,
'd6.py' ]
 
"""获取文件的权限"""
In [ 20 ] : import os
 
In [ 21 ] : os . stat ( 'bd.py' )
Out [ 21 ] : os . stat_result ( st_mode = 33188 , st_ino = 8596213658 , st_dev = 16777220 , st_nlink = 1 , st_uid = 501 , st_gid = 20 , st_size = 1377 , st_atime = 1510551606 , st_mtime = 1510551605 , st_ctime = 1510551605 )
 
In [ 22 ] : os . stat ( 'bd.py' ) . st_mode
Out [ 22 ] : 33188
 
In [ 23 ] : oct ( os . stat ( 'bd.py' ) . st_mode )
Out [ 23 ] : '0o100644'
 
import os
import stat # 获取文件的状态
# 获得当前文件下目录
file_l = os . listdir ( '.' )
# startswith中参数必须是元组形式,只有满足一个条件,返回True
target_file = [ name for name in file_l if name . startswith ( ( '.sh' , '.py' ) ) ]
for file in target_file :
     # 给满足条件的文件所有者赋予执行权限
     os . chmod ( file , os . stat ( file ) . st_mode | stat . S_IXUSR )
     # os.stat(file).st_mode | stat.S_IXUSR 获取执行的权限



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值