file2utf8

# IPython log file

import chardet
import os

def getEncoding(file_name):
    with open(file_name, 'rb') as f:
        return chardet.detect(f.read())['encoding']

def file2utf8():
    L = [i for i in os.listdir() if os.path.splitext(i)[-1] in ['.txt', '.py'] and os.path.isfile(i)]
    if not os.path.exists('outdir'):
        os.mkdir('outdir')
    if not os.path.exists('report'):
        os.mkdir('report')
    for file_name in L:
        with open(file_name, 'r', encoding = getEncoding(file_name)) as f:
            s = f.read()
        with open(os.path.join('outdir',file_name), 'w', encoding = 'utf-8') as f:
            f.write(s)
    with open(os.path.join('report','report.txt'),'w',encoding='utf-8') as f:
        for i in L:
            f.write(' '.join([i,getEncoding(i),'\n']))

if __name__=='__main__':
    file2utf8()
全部评论

相关推荐

葬爱~冷少:我当时都是上午刷力扣,下午背八股,有活给我先别急,没活就干自己的事情
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务