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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -