2

The function:

def write_file():
    f = open('image.txt', 'wb')
    byte_arr = getbytes(read_bits())
    byte_list = list(byte_arr)
    print(byte_list)
    some_bytes = bytearray(byte_list)
    print(bytes(some_bytes))
    st = bytes(some_bytes).decode('latin1')
    immutable_bytes = bytes(some_bytes)
    with open('test','wb') as f:
         f.write(immutable_bytes )

The output is like:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00'

I want to write string value converted from this list of byte values.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

2

You could try this:

st = bytes(some_bytes).decode('utf-8')
with open('test','wb') as f:
     f.write(st)

instead of this:

st = bytes(some_bytes).decode('latin1')
immutable_bytes = bytes(some_bytes)
with open('test','wb') as f:
     f.write(immutable_bytes )