def skipBOM(fd):
"""
Removes the BOM from UTF-8 files so that we can live in peace.
:param fd: The file descriptor that may or may not have a BOM at the start.
:return: The position after the BOM as reported by fd.tell().
"""
try:
pos = fd.tell()
except IOError:
return 0
if isinstance(fd, io.TextIOBase):
fst = fd.read(len(codecs.BOM_UTF8.decode('utf-8')))
if fst.encode('utf-8') != codecs.BOM_UTF8:
fd.seek(pos)
else:
fst = fd.read(len(codecs.BOM_UTF8))
if fst != codecs.BOM_UTF8:
fd.seek(pos)
return fd.tell()
评论列表
文章目录