有人可以告诉我如何编码return语句,以便它可以解码它。或者需要更改以获得编码值。
码
def run_process(cmd_args):
with subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
return proc.communicate()
res = run_process(cmd_args);
print(res)
print(res.decode("utf-8"))
产量
print(res.decode("utf-8"))
AttributeError: 'tuple' object has no attribute 'decode'
Popen.communicate
返回(stdout, stderr)
的元组,因此您需要将返回值视为:
stdout, stderr = run_process(cmd_args);
print(stdout)
print(stdout.decode("utf-8"))
请阅读Popen.communicate
's documentation了解更多详情。