Writing integer to binary file
Python comes with a built-in pickle module for serialization:

&gt;&gt;&gt; import pickle
&gt;&gt;&gt; with open('data', 'wb') as f: pickle.dump(9 ** 33, f)
&gt;&gt;&gt; with open('data', 'rb') as f: print(pickle.load(f))
30903154382632612361920641803529


(P.S. I wouldn't use a filename ending with .txt to store binary data.)