[Solved] Can’t concat bytes to str

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')

The output file looks like:

test string

and then I get this error:

b'decryption successfuln'
Traceback (most recent call last):
  File ".../", line 36, in <module>
    f.write (plaintext + 'n')
TypeError: can't concat bytes to str

Solution #1:

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')

Respondent: Wooble

Solution #2:

subprocess.check_output() returns bytes.

so you need to convert ‘n’ to bytes as well:

 f.write (plaintext + b'n')

hope this helps

Respondent: HISI

Solution #3:

You can convert type of plaintext to string:

f.write(str(plaintext) + 'n')
Respondent: gcs

Solution #4:

Respondent: Ravi Prakash

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy