UTF-8编码去掉BOM头方法

时间:2021-08-05 22:26:16
# -*- coding: utf-8 -*- #encoding=utf-8 import os
import codecs

def utf8 (path):
f = open(path,"r")
s = f.read()
f.close()

if s.startswith(codecs.BOM_UTF8):
s = s[len(codecs.BOM_UTF8):]
f = open(path, "w")
f.write(s)
f.flush()
f.close()


def getListFiles(path):
assert os.path.isdir(path), '%s not exist.' % path
ret = []
for root, dirs, files in os.walk(path):
#print '%s, %s, %s' % (root, dirs, files) for filespath in files:
ret.append(os.path.join(root,filespath))
return ret


ret = getListFiles('d:\src')  //源码的目录
print len(ret)
for in ret:
print f,"\n" utf8(f)