我是Python新手。我的问题是:
(一)ShellHelper.py公司名称:
import subprocess
def execute_shell(shell):
process = subprocess.Popen(shell, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
exit_code = process.returncode
if exit_code == 0:
return output
else:
raise Exception(shell, exit_code, output)
(二)启动器.py在
^{pr2}$c)我的终端:
pc19:AutomationTestSuperviser F1sherKK$ python3 Launcher.py
Enter shell command: ls
[b'Launcher.py', b'ShellHelper.py', b'__pycache__']
b'
?在
解码输出以将字节字符串转换为“常规”文本。列表是由
split
创建的,您可以join
使用空格字符创建正常的ls
输出:要提供更明确的答案,请考虑以下几点:
1)进程的输出不是ASCII格式的,因此文件开头的b表示字符串是二进制格式。在
2)选择将列表返回到打印函数,如下所示:
这将在单独的行中打印每一行:
^{pr2}$相关问题 更多 >
编程相关推荐