bytes casted to int by a list constructor
<p>I want to read <em>n</em> bytes a binary file and store each byte in a list. However, when I do so, each byte is casted into an <code>int</code>.</p>

<p>First I create a random file.</p>

<pre><code>$ head -c 16 &gt; random.file
</code></pre>

<p>Then I try to read it:</p>

<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; with open('random.file', 'rb') as fp:
...     l = list(fp.read(8))
...     print(fp.read(8))
... 
b'@\xc2\xdf\x9f\xbbv\xa1\x90'
&gt;&gt;&gt; l
[79, 233, 19, 116, 252, 50, 248, 144]
&gt;&gt;&gt; type(l[0])
&lt;class 'int'&gt;
</code></pre>

<p>So I understand that my byte value cannot go over <code>255</code> however the fact that Python is automatically casting is causing issues since don't get the expected type.</p>

<p>Is that normal? How can I avoid this?</p>

<p>Any single byte you access in a list of bytes is just a single int value:</p>

<pre><code>&gt;&gt;&gt; b"foo"[0]
102
</code></pre>

<p>If you want to create a new list that consists of lists of bytes with the length 1, you'll have to do that explicitly:</p>

<pre><code>&gt;&gt;&gt; c = b"foo"
&gt;&gt;&gt; l = []
&gt;&gt;&gt; for i in range(0, len(c)):
...   l.append(c[i:i+1])
&gt;&gt;&gt; l
[b'f', b'o', b'o']
</code></pre>

<p>But I'm not really sure I see any reason for doing that, as accessing the single values, which is now embedded inside a new list, will just, again, return an int.</p>

python types casting