INNER CODE UNIT · Python
read_file
Cooler2/ApusGameEngine · tools/normalize_indent.py:285
def read_file(path):
"""Returns (bom, crlf, lines) or raises."""
with open(path, 'rb') as f:
raw = f.read()
bom = raw[:3] == b'\xef\xbb\xbf'
data = raw[3:] if bom else raw
text = data.decode('utf-8', errors='replace')
crlf = '\r\n' in text
lines = text.splitlines()
return bom, crlf, lines
def write_file(path, bom, crlf, lines):
sep = '\r\n' if crlf else '\n'
text = sep.join(lines)
raw = (b'\xef\xbb\xbf' if bom else b'') + text.encode('utf-8')
with open(path, 'wb') as f:
f.write(raw)