[Tutor] How to read all integers from a binary file?
Laura Creighton
lac at openend.se
Fri Oct 9 20:57:15 CEST 2015
In a message of Fri, 09 Oct 2015 15:02:20 -0000, David Aldrich writes:
>Hi
>
>I have a binary file of 32-bit unsigned integers. I want to read all those integers into a list.
>
>I have:
>
> ulData = []
> with open(ulBinFileName, "rb") as inf:
> ulData.append(struct.unpack('i', inf.read(4)))
>
>This obviously reads only one integer from the file. How would I modify this code to read all integers from the file please?
>
>Best regards
>
>David
You did the hard part, finding the struct module:
and I assume that you got struct_unpack defined somewhere
You need to wrap that up in:
with open(ulBinFileName, "rb") as inf:
while True:
data = inf.read(4)
if not data:
break
ulData.append(struct.unpack('i', data))
Laura
More information about the Tutor
mailing list