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 Python 3, strings are implicitly Unicode and are therefore detached from a specific binary representation (which depends on the encoding that is used).  Therefore, the string <code>"%s" % y</code> cannot be written to a file that is opened in binary mode.</p>

<p>Instead, you can just write <code>y</code> directly to the file:</p>

<pre><code>y = bytearray.fromhex('{:0192x}'.format(big_int))
f.write(y)
</code></pre>

<p>Moreover, your code (<code>"%s" % y</code>) in fact creates a Unicode string containing the <em>string representation</em> of <code>y</code> (i.e., <code>str(y)</code>), which isn't what you think it is.  For example:</p>

<pre><code>&gt;&gt;&gt; '%s' % bytearray()
"bytearray(b'')"
</code></pre>

python