有时候编码问题在导入工程的时候很烦,所以还是让世界都是"UTF-8"吧。
抄来一段代码:
#!/usr/env python
# -*- coding: utf8 -*-
import fnmatch
import os
import sys
import codecs
import chardet def find_files(path, fnexp="*"):
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, fnexp):
yield os.path.join(root, filename) def ReadFile(filePath,encoding="gbk"):
with codecs.open(filePath,"r",encoding) as f:
return f.read() def WriteFile(filePath,u,encoding="utf-8"):
with codecs.open(filePath,"w",encoding) as f:
f.write(u) def GBK_2_UTF8(src,dst):
content = ReadFile(src,encoding="gbk")
WriteFile(dst,content,encoding="utf-8") def UTF8_2_GBK(src,dst):
content = ReadFile(src,encoding="utf-8")
WriteFile(dst,content,encoding="gbk") def trans(fpath):
for fn in find_files(fpath):
print fn
d = chardet.detect(open(fn,"r").read())
print d
if d['encoding'] != 'utf-8':
GBK_2_UTF8(fn,fn)
print "ok" if __name__ == '__main__':
if len(sys.argv) > 1 :
for fpath in sys.argv[1:]:
trans(fpath)
else:
fpath = raw_input("path:")
trans(fpath)