I need to create file of 10,000 random integers for testing. I will be using the file in Python and C, so I can't have the data represented as strings because I don't want the extra overhead of integer conversion in C.
In Python I can use struct.unpack
to convert the file to integer, but I can't use the write()
method to write that to a file for use in C.
Is there any way in Python to write just integers, not integers-as-strings, to a file? I have used print(val, file=f)
and f.write(str(val))
, but in both cases it writes a string.
Here is where I am now:
file_root = "[ file root ]"
file_name = file_root + "Random_int64"
if os.path.exists(file_name):
f = open(file_name, "wb")
f.seek(0)
for _ in range(10000):
val = random.randint(0, 10000)
f.write(bytes(val))
f.close()
f = open(file_name, "rb")
wholefile = f.read()
struct.unpack(wholefile, I)
My unpack
format string is wrong, so I am working on that now. I'm not that familiar with struct.unpack
.