- 论坛徽章:
- 0
|
我试了下,在每个文件头加入‘# -*- coding: utf-8 -*-’即可,因为文件太多了,就写了个脚本do.py,可放在同一目录下
运行:python do.py 即可在每个文件头加入此句
import os
chinese = '''# -*- coding: utf-8 -*-
'''
ch = ''
skip = 0
t1 = '# -*-'
t2 = '#-*-'
rootdir = os.getcwd()
for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
if filename != 'do.py':
f = open(filename)
while True:
line = f.readline()
if line:
if line.startswith(t1) or line.startswith(t2):
skip = 1
break
else:
break
if skip == 1:
f.close()
break
f.seek(0)
temp = f.read()
ch = chinese + temp
f.close()
f = open(filename,'w')
f.write(ch)
f.close()
ch = ''
可多次运行,但只加入一次
|
|