I\'m trying to use python to create a random binary file. This is what I\'ve got already:
f = open(filename,\'wb\')
for i in xrange(size_kb):
for ii in xrang
IMHO - the following is completely redundant:
f.write(struct.pack("=I",random.randint(0,sys.maxint*2+1)))
There's absolutely no need to use struct.pack
, just do something like:
import os
with open('output_file', 'wb') as fout:
fout.write(os.urandom(1024)) # replace 1024 with size_kb if not unreasonably large
Then, if you need to re-use the file for reading integers, then struct.unpack
then.
(my use case is generating a file for a unit test so I just need a file that isn't identical with other generated files).
Another option is to just write a UUID4 to the file, but since I don't know the exact use case, I'm not sure that's viable.