The below code works perfectly for python 2.7.13
import os with open(\'random.bin\',\'w\') as f: f.write(os.urandom(10))
But throws error f
In Python 3 it makes a difference whether you open the file in binary or text mode. Just add the b flag to make it binary:
b
with open('random.bin','wb') as f:
This works in Python 2 too.