def guess_encoding(path):
    """Sample a file, seeing if the platform-dependent encoding works, and
       trying latin_1 if it doesn't.

       A more robust solution would be to use the chardet library,
       but we want to try and keep this dependency-free."""

    try:
        e = None
        with open(path, newline='', encoding=e) as f:
            f.readline()
        return e
    except UnicodeDecodeError:
        e = 'latin_1'
        with open(path, newline='', encoding=e) as f:
            f.readline()
        return e