-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpyfilemode.py
More file actions
executable file
·32 lines (26 loc) · 929 Bytes
/
pyfilemode.py
File metadata and controls
executable file
·32 lines (26 loc) · 929 Bytes
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import stat
def pyfile_config(path):
for abPath in [os.path.join(path, x) for x in os.listdir(path)]:
if os.path.splitext(abPath)[1] == '.py':
change_mode(abPath)
add_annotation(abPath)
elif os.path.isdir(abPath):
pyfile_config(abPath)
# 修改权限
def change_mode(path):
os.chmod(path, stat.S_IRWXU | stat.S_IXGRP | stat.S_IRGRP | stat.S_IROTH | stat.S_IXOTH) # 755权限
# 添加注释
def add_annotation(path):
with open(path, 'r+', encoding='utf-8') as f:
if not f.readline().startswith('#!/usr/bin/env python3'):
f.seek(0, 0)
a = f.readlines()
a.insert(0, '#!/usr/bin/env python3\n')
a.insert(1, '# -*- coding: utf-8 -*-\n')
s = ''.join(a)
f.seek(0, 0)
f.write(s)
print('success-->', path)