
正文
py2.7 批量转换文件为 utf8 编码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
source insight 不支持 utf8 ,但是在 linux 上查看的时候是 utf8 编码,就会显示不正常,所以写了个 python 小脚本,可以批量转换
py2.7
#coding:utf-8
'''
GBK 转 UTF-8 工具
author: 宁次
date :2017-02-03 19:58
用法:python toutf8.py d:/wwwwroot
'''
import sys
import os
#要转换的文件类型
exts = ('.c', '.cpp', '.s', '.S', '.lds', '.h', 'Makefile')
if 2 > len(sys.argv):
print 'usage:python toutf8.py d:/wwwroot'
sys.exit()
#命令行传来的需要处理的路径
path = sys.argv[1]
for root, dirs, files in os.walk(path):
for name in files:
ext = os.path.splitext(name)[1]
if ext in exts:
file = os.path.join(root, name)
with open(file, 'r+') as f:
data = f.read()
data = data.decode('GBK').encode('UTF-8')
f.seek(0)
f.write(data)





