美文网首页
批量去除 utf-8的BOM

批量去除 utf-8的BOM

作者: 62ba53cbc93c | 来源:发表于2018-07-25 10:56 被阅读0次
    import os
    
    
    def removeBom(file):
        '''移除UTF-8文件的BOM字节'''
        BOM = b'\xef\xbb\xbf'
        existBom = lambda s: True if s == BOM else False
    
        f = open(file, 'rb')
        if existBom(f.read(3)):
            fbody = f.read()
            # f.close()
            with open(file, 'wb') as f:
                f.write(fbody)
    
    
    if __name__ == '__main__':
        for root, dirs, files in os.walk("D:\\PycharmProjects\\kscnn\\political"):
            count = 0
            for file in files:
                #if file.find(".txt") != -1:
                removeBom(os.path.join(root, file))
                count += 1
            print(count)
    

    相关文章

      网友评论

          本文标题:批量去除 utf-8的BOM

          本文链接:https://www.haomeiwen.com/subject/rqqpmftx.html