事实证明,这是一个向python的粗略过渡。这是怎么回事?以下内容:
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()
输出文件如下所示:
test string
然后我得到这个错误:
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
可以将
plaintext
的类型转换为字符串:subprocess.check_output()
返回bytestring。在Python 3中,unicode(
str
)对象和bytes
对象之间没有隐式转换。如果你知道输出的编码,你可以.decode()
它得到一个字符串,或者你可以用"\n".encode('ascii')
将要添加到bytes
中的\n
subprocess.check_output()返回字节。
所以您还需要将'\n'转换为字节:
希望这有帮助
相关问题 更多 >
编程相关推荐