Storing a random byte string in Python -
for project, need able store random byte strings in file , read byte string again later. example, want store randombytestring following code:
>>> os import urandom >>> randombytestring=urandom(8) >>> randombytestring b'zoz\x84\xfb\xcem~'
what proper way this?
edit: forgot mention want store 'normal' string alongside byte strings.
code like:
>>> fh = open("e:\\test","wb") >>> fh.write(randombytestring) 8 >>> fh.close()
operate file binary mode. also, in better manner if file operations near 1 place (thanks @blender):
>>> open("e:\\test","wb") fh: fh.write(randombytestring)
update: if want strong normal strings, encode , write like:
>>> "test".encode() b'test' >>> fh.write("test".encode())
here fh means same file handle opened previously.
Comments
Post a Comment