utf8_to_gbk.py

#!/usr/bin/env python
#encoding=gbk
import sys
reload(sys)
sys.setdefaultencoding("gbk")

"""
将指定目录下的所有文件从utf-8转换到gbk

主要用于查看sourceinsight的代码

sourceinsight需要gbk,utf-8会乱码


"""
import codecs,os,os.path
def utf8_to_gbk(file):
    print file
    originals=codecs.open(file,"r","utf-8").readlines()
    f=codecs.open(file,"w","gbk")
    for line in originals:
        f.write(line.encode("gbk","ignore"))
    f.close()

def walk(path):
     for root, dirs, files in os.walk(path):
        for file in files:
            utf8_to_gbk(os.path.join(root,file))

if __name__ == '__main__':
    #file=ur"C:\mao\code\trunk\query\QueryServer\src\PinYin.h"
    #print file
    #utf8_to_gbk(file)
    walk(r"C:\mao\code\trunk\query\QueryServer\src")

你可能感兴趣的:(utf8)