批量转化文件夹下脚本或者脚本文件为UTF8格式

 # -- coding: UTF-8 --
import os,sys
import chardet
 
def codeswith( filename,out_enc="UTF8"):
    try:
        if os.path.splitext(filename)[1] == '.cs':
            content = open(filename).read()
            result = chardet.detect(content)#通过chardet.detect获取当前文件的编码格式串,返回类型为字典类型
            coding = result.get('encoding')#获取encoding的值[编码格式]
            if coding != 'utf-8':#文件格式如果不是utf-8的时候,才进行转码
                new_content = content.decode(coding).encode(out_enc)
                open(filename, 'w').write(new_content)
                print filename +"->"+coding + "<===> UTF8! \n", 
            else:
                print filename + " Is UTF8\n",
    except IOError,e:
        print " error"
 
def explore(dir):
    for root, dirs, files in os.walk(dir):
        for file in files:
            path = os.path.join(root, file)
            codeswith(path)
 
def main():
    for path in sys.argv[1:]:
        if os.path.isfile(path):
            codeswith(path)
        elif os.path.isdir(path):
            explore(path)
 
if __name__ == "__main__":
    main()

p'ython是2.7版本的

其中要使用

charted库 安装方法pip install charted

pip安装方法 each_install pip


运行的例子如: python switch.py C:/unity/project/ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值