Reading entire binary file into python
I need to import a binary file from Python -- the contents are signed 16-bit integer, big endian. 

The following threads suggest how to pull in sevaral bytes at a time, but is this the way to scale up to read in a whole file?

[http://stackoverflow.com/questions/1035340/reading-binary-file-in-python][1]

[http://stackoverflow.com/questions/875046/recieving-16-bit-integers-in-python][2]

I thought to create a function like 

    from numpy import *
    import os

    def readmyfile(filename,bytes=2,endian='>h'):
        totalBytes = os.path.getsize(filename)
        values = empty(totalBytes/bytes)
        with open(filename,'rb') as f:
            for i in range(len(values)):
                values[i] = struct.unpack(endian,f.read(bytes))[0]
        return values
    
    filecontents = readmyfile('filename')

but this is quite slow (file is 165924350 bytes). Is there a better way? Thanks much ~


  [1]: http://stackoverflow.com/questions/1035340/reading-binary-file-in-python
  [2]: http://stackoverflow.com/questions/875046/recieving-16-bit-integers-in-python


Reading entire binary file into python
===

I need to import a binary file from Python -- the contents are signed 16-bit integer, big endian. 

The following threads suggest how to pull in sevaral bytes at a time, but is this the way to scale up to read in a whole file?

[http://stackoverflow.com/questions/1035340/reading-binary-file-in-python][1]

[http://stackoverflow.com/questions/875046/recieving-16-bit-integers-in-python][2]

I thought to create a function like 

    from numpy import *
    import os

    def readmyfile(filename,bytes=2,endian='>h'):
        totalBytes = os.path.getsize(filename)
        values = empty(totalBytes/bytes)
        with open(filename,'rb') as f:
            for i in range(len(values)):
                values[i] = struct.unpack(endian,f.read(bytes))[0]
        return values
    
    filecontents = readmyfile('filename')

but this is quite slow (file is 165924350 bytes). Is there a better way? Thanks much ~


  [1]: http://stackoverflow.com/questions/1035340/reading-binary-file-in-python
  [2]: http://stackoverflow.com/questions/875046/recieving-16-bit-integers-in-python

