Get file encoding of a text file

def predict_encoding(file_path, n_lines=20):
    '''Get file encoding of a text file'''
    import chardet

    # Open the file as binary data
    with open(file_path, 'rb') as f:
        # Join binary lines for specified number of lines
        rawdata = b''.join([f.readline() for _ in range(n_lines)])

    return chardet.detect(rawdata)['encoding']