Q:

Python: How can I convert an int to a 32-bit size_t and write that to a binary file

I'm trying to edit.bin in python files to run through another program that requires the first number to be a 32-bit size_t. I'm using a 64 bit version of python 3. Below is what I've tried so far.
with open(outputFileTrain, 'wb') as binFile:
     file2Len = ctypes.c_int_32 (file2Len)
     z = struct.pack('i',file2Len)
     binFile.write(z)
     binFile.close()

Thanks

A:

So, assuming you really need 32-bit (size_t may or may not be 32-bit), signed, struct is the module you need.  It will convert the int to bytes which can then be written to a file opened in binary mode.
with open(outputFileTrain, 'wb') as binFile:
    z = struct.pack('i',file2Len)
    binFile.write(z)

If the cint should be unsigned (probably the case if the other program uses it as a size_t, use struct.pack('I',... instead.

