Search code examples
pythonfileoutputpack

Error in using `struct.pack` for writing data to file


I have a numpy.ndarray sample of numbers, each between 1 and 2**20.

I'd like to write it into a binary file, such that each element is represented by four bytes.

However, the resulting file size is different from 4 times the size of the sample.

This is the code I'm using:

        outputFile = open('testDS', 'w')
        print len(sample)
        if (outputFile is not None):
            for s in sample:
                assert(s < 2**20)
                r = struct.pack("i", s)
                assert(len(r) == 4)
                outputFile.write(r)
        outputFile.close()

The output I'm getting (the size of the sample) is: 1000

However, the resulting file size is 4026 bytes.

Any ideas why the file size is not exactly 4000 bytes?


Solution

  • Open the file in binary mode:

    outputFile = open('testDS', 'wb')
    

    Otherwise, the file object may do some magic translation of newline characters that show up in your binary data, resulting in additional characters being written to the file. See, for example, https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files