This is proving to be a rough transition over to python. What is going on here?:
f = open( 'myfile', 'a+' ) f.write('test string' + '\n') key = "pass:hello" plaintext = subprocess.check_output(['openssl', 'aes-128-cbc', '-d', '-in', test, '-base64', '-pass', key]) print (plaintext) f.write (plaintext + '\n') f.close()
The output file looks like:
test string
and then I get this error:
b'decryption successful\n' Traceback (most recent call last): File ".../Project.py", line 36, in <module> f.write (plaintext + '\n') TypeError: can't concat bytes to str
The Python "TypeError: can't concat str to bytes" occurs when we try to concatenate a bytes object and a string. To solve the error, decode the bytes object into a string before concatenating the strings.
The Python "TypeError: can only concatenate str (not "bytes") to str" occurs when we try to concatenate a string and a bytes object. To solve the error, decode the bytes object into a string before concatenating the strings.
Python concatenate strings and bytes To concatenate strings and bytes we will use the + operator to concatenate, and also we use str() to convert the bytes to string type, and then it will be concatenated. To get the output, I have used print(my_str + str(bytes)).
subprocess.check_output()
returns a bytestring.
In Python 3, there's no implicit conversion between unicode (str
) objects and bytes
objects. If you know the encoding of the output, you can .decode()
it to get a string, or you can turn the \n
you want to add to bytes
with "\n".encode('ascii')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With