I have the following problem. When I read the bytes of an file via
with open(r'file-path', 'rb') as file: data = file.read() and write this byte object "data" into in other file using 'wb'. The out coming file is fine and there are no problems. But when I first convert the byte object "data" into a string data_str = str(data) and than convert it back using new_data = data_str.encode() "new_data" looks exactly the same as "data", but if I write "new_data" into a file the file isn´t working. Or as in my case not recognized as being an mp3 file, even though the bytes are the same on the first look.
I know it seems useless to convert the bytes into a string and than back into bytes, but in my case I really have to do that.
data_str. You'll see that it has literalb'...'around it. That's not the same as the original data.str(data)is returning the printed representation of the byte string, not a string containing the bytes themselves.data != new_data.str()is NOT the way to convert bytes to strings. Why do you think you need convert to a string? Bytes strings are for binary data. Unicode strings are for text. If you need the binary data as text, you may need a binary to text converter (uuencode, base64, hexdump). This is an XY problem. Describe why you need to do this.