批量去除 utf-8的BOM

2018-07-25  本文已影响0人  62ba53cbc93c
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)
上一篇下一篇

猜你喜欢

热点阅读