Python子进程检查输出不工作

2024-04-27 02:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着做我的测试_脚本.py总的来说_脚本.py包含子流程。试验_脚本.py是一个小口和主程序_脚本.py应该用2个参数调用它,并捕获输出。代码如下:

测试_脚本.py

a = int(input())
b = int(input())

print(a+b)

main_脚本.py

^{pr2}$

这是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/John/Desktop/main_script.py", line 2, in <module>
    subprocess.check_output(['python', 'test_script.py', 2,3])
  File "C:\Python34\lib\subprocess.py", line 607, in check_output
    with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
  File "C:\Python34\lib\subprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1085, in _execute_child
    args = list2cmdline(args)
  File "C:\Python34\lib\subprocess.py", line 663, in list2cmdline
    needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable

Tags: inpy脚本inputoutputmainlibcheck
2条回答

{a1}

import subprocess                                                                      
subprocess.check_output("python test_script.py", stderr=subprocess.STDOUT, shell=True)

参数的所有部分都必须是字符串。请执行以下操作:

subprocess.check_output(['python', 'test_script.py', "2", "3"])

如果这个命令无法运行,您将得到一个异常。要捕获它并查看输出:

^{pr2}$

第二个脚本将失败,因为它需要来自stdin的输入,而主脚本正在发送2和{}作为参数。调查sys.argv

相关问题 更多 >