UTF-8符号化BOMヘッド除去方法

2910 ワード

# -*- 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,"
" utf8(f)