

def determine_encoding(path, default=None):
    'Determines the encoding of a file based on byte order marks.\n\n    Arguments:\n        path (str): The path to the file.\n        default (str, optional): The encoding to return if the byte-order-mark\n            lookup does not return an answer.\n\n    Returns:\n        str: The encoding of the file.\n\n    '
    byte_order_marks = (('utf-8-sig', (codecs.BOM_UTF8,)), ('utf-16', (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)), ('utf-32', (codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)))
    try:
        with open(path, 'rb') as infile:
            raw = infile.read(4)
    except IOError:
        return default
    for (encoding, boms) in byte_order_marks:
        if any((raw.startswith(bom) for bom in boms)):
            return encoding
    return default

dataset/ETHPy150Open c-w/Gutenberg/gutenberg/_util/os.py determine_encoding/original