Python, how to put 32-bit integer into byte array
<p>I usually perform things like this in C++, but I'm using python to write a quick script and I've run into a wall.</p>

<p>If I have a binary list (or whatever python stores the result of an "fread" in).  I can access the individual bytes in it with: buffer[0], buffer[1], etc.</p>

<p>I need to change the bytes [8-11] to hold a new 32-bit file-size (read: there's already a filesize there, I need to update it).  In C++ I would just get a pointer to the location and cast it to store the integer, but with python I suddenly realized I have no idea how to do something like this.</p>

<p>How can I update 4 bytes in my buffer at a specific location to hold the value of an integer in python?</p>

<p><strong>EDIT</strong></p>

<p>I'm going to add more because I can't seem to figure it out from the solutions (though I can see they're on the right track).</p>

<p>First of all, I'm on python 2.4 (and can't upgrade, big corporation servers) - so that apparently limits my options.  Sorry for not mentioning that earlier, I wasn't aware it had so many less features.</p>

<p>Secondly, let's make this ultra-simple.</p>

<p>Lets say I have a binary file named 'myfile.binary' with the five-byte contents '4C53535353' in hex - this equates to the ascii representations for letters "L and 4xS" being alone in the file.</p>

<p>If I do:</p>

<pre><code>f = open('myfile.binary', 'rb')
contents = f.read(5)
</code></pre>

<p>contents should (from Sven Marnach's answer) hold a five-byte immutable string.</p>

<p>Using Python 2.4 facilities only, how could I change the 4 S's held in 'contents' to an arbitrary integer value? I.e. give me a line of code that can make byte indices contents [1-4] contain the 32-bit integer 'myint' with value 12345678910.</p>
