def detect_file_encoding(filename):
with open(filename, '<STR_LIT:rb>') as opened:<EOL><INDENT>sample = opened.read(<NUM_LIT:2> ** <NUM_LIT:20>)<EOL>_, encoding = ftfy.guess_bytes(sample)<EOL>return encoding<EOL><DEDENT>
Use ftfy to detect the encoding of a file, based on a sample of its
first megabyte.

ftfy's encoding detector is limited. The only encodings it can detect are
UTF-8, CESU-8, UTF-16, Windows-1252, and occasionally MacRoman. But it
does much better than chardet.