How to solve TypeError: 'str' does not support the buffer interface?
<p>My code:</p>

<pre><code>big_int = 536870912
f = open('sample.txt', 'wb')

for item in range(0, 10):
   y = bytearray.fromhex('{:0192x}'.format(big_int))
   f.write("%s" %y)
f.close()
</code></pre>

<p>I want to convert a long int to bytes. But I am getting <code>TypeError: 'str' does not support the buffer interface</code>. </p>

<p>In addition to @Will answer, it is better to use <a href="http://effbot.org/zone/python-with-statement.htm" rel="nofollow"><code>with</code></a> statement in to open your files. Also, if you using python 3.2 and later, you can use <a href="https://docs.python.org/3/library/stdtypes.html#int.to_bytes" rel="nofollow"><code>int.to_bytes</code></a> or <a href="https://docs.python.org/3/library/stdtypes.html#int.from_bytes" rel="nofollow"><code>int.from_bytes</code></a> to reverse the process.</p>

<p>Sample of what I said:</p>

<pre><code>big_int=536870912

with open('sample.txt', 'wb') as f:
    y = big_int.to_bytes((big_int.bit_length() // 8) + 1, byteorder='big')
    f.write(y)
    print(int.from_bytes(y, byteorder='big'))
</code></pre>

<p>The last <code>print</code> is to show you how to reverse it.</p>

python