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 获取执行的权限
|