Python 3-TypeError:只能将str(不是“ bytes”)连接到str

时间:2019-07-23 21:57:55

标签: python python-3.x windows

我输入了错误的代码,找不到修复程序。

49         os.system('powershell -enc 
    '+base64.b64encode(addpermissions.encode('utf_16_le')))   
    . 
    .
    .
     137 HexRegHash, HexRegSysk, jd, skew1, gbg, data = getRegistryValues(HexRID)

我遇到此错误:

    Traceback (most recent call last):
      File "hash.py", line 137, in <module>
        HexRegHash, HexRegSysk, jd, skew1, gbg, data = 
    getRegistryValues(HexRID)
      File "hash.py", line 49, in getRegistryValues
        os.system('powershell -enc 
    '+base64.b64encode(addpermissions.encode('utf_16_le')))
    TypeError: can only concatenate str (not "bytes") to str

1 个答案:

答案 0 :(得分:0)

base64.b64encode产生字节流,而不是字符串。因此,要进行串联,必须先使用str(base64.b64encode(addpermissions.encode('utf_16_le')))

将其转换为字符串
b64cmd = base64.b64encode(cmd.encode('utf_16_le')).decode('utf-8')
os.system('powershell -enc ' + b64cmd)

编辑:正常的字符串转换不适用于os.system,而是使用decode('utf-8')

相关问题