If I call os.urandom(64), I am given 64 random bytes. With reference to Convert bytes to a Python string I tried

a = os.urandom(64)

but got the traceback error stating that the bytes are not in utf-8.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte

with the bytes


Is there a fullproof method to decode these bytes into some string representation? I am generating sudo random tokens to keep track of related documents across multiple database engines.

The code below will work on both Python 2.7 and 3:

from base64 import b64encode
from os import urandom

random_bytes = urandom(64)
token = b64encode(random_bytes).decode('utf-8')

You have random bytes; I’d be very surprised if that ever was decodable to a string.

If you have to have a unicode string, decode from Latin-1:


because it maps bytes one-on-one to corresponding Unicode code points.

You can use base-64 encoding. In this case:

a = os.urandom(64)

Also note that I’m using encode here rather than decode, as decode is trying to take it from whatever format you specify into unicode. So in your example, you’re treating the random bytes as if they form a valid utf-8 string, which is rarely going to be the case with random bytes.

this easy way:

a = str(os.urandom(64))
print(F"the: {a}")

Are you sure that you need 64 bytes represented as string?

Maybe what you really need is N-bits token?
If so, use secrets. The secrets module provides functions for generating secure tokens, suitable for applications such as password resets, hard-to-guess URLs, and similar.

import secrets

>>> secrets.token_bytes(16)  

>>> secrets.token_hex(16)  

>>> secrets.token_urlsafe(16)  

Or Maybe you need 64 chars length random string? import string

import secrets
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(64))