infinity1207
2015-12-23 13:28:42 +08:00
用 python 写了一个,你根据需要把需要 top 变量改为你需要转换编码源文件所在目录即可。
```
# -*- coding: utf-8 -*-
import os
import binascii
import re
def handle(file_path):
f = open(file_path, 'r')
content = f.read()
f.close()
bom = binascii.b2a_hex(content[:3])
if bom == 'efbbbf':
print "%s \t have utf8 bom already." % file_path
return;
f = open(file_path, 'w')
bom = binascii.a2b_hex('efbbbf')
content = bom + content
f.write(content)
f.close()
print "%s \t was added utf8 bom done." % file_path
if __name__=='__main__':
top = r'd:\\data'
for root, dirs, files in os.walk(top):
for f in files:
m = re.search(r'(\.h)|(\.cpp)|(\.hpp)', f)
if m:
file_path = os.path.join(root, f)
handle(file_path)
```