我尝试使用subprocess.popen并且效果很好。 唯一的问题是当我输入例如" iffconfig"时,我的整个脚本崩溃了。而不是" ifconfig" 如何确保它不会在错误的命令上崩溃,而只是回复错误消息?请指出我正确的方向。 示例代码:
command = "ifconfig" #this command is actually requested so you can make a typo which causes it to crash
temp = subprocess.Popen(command, stdout=subprocess.PIPE)
out = temp.stdout.read()
out.replace("\r", "\n")
outlist = out.split("\n")
for line in outlist:
print(line)
非常感谢任何帮助:)