def reverse_bits(x):
    return int('{:08b}'.format(x)[::-1], 2)
reverse_table = bytes.maketrans(bytes(list(range(0, 256))), bytes(reverse_bits(x) for
    x in range(0, 256)))
with open('input.txt', 'rb') as f_input:
    with open('output.txt', 'wb') as f_output:
        data = f_input.read()
f_output.write(data.translate(reverse_table))
Efficient bitswapping in python3