Python3.5子进程错误

问题描述 投票:0回答:1

我使用下面的函数逐行读取我的python脚本输出并保存并行。但最终得到Traceback错误。

码:

def myrun(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout = []
    while True:
        line = p.stdout.readline()
        stdout.append(line)
        print (line),
        if line == '' and p.poll() != None:
            break
    return ''.join(stdout)

当呼叫功能为:

myrun(os.system("./tests_run.py"))

我得到以下错误:

错误:

Traceback (most recent call last):
  File "./Portability_Tests.py", line 38, in <module>
    myrun(os.system("./tests_run.py"))
  File "./Tests.py", line 11, in myrun
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  File "/usr/local/lib/python3.5/subprocess.py", line 676, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.5/subprocess.py", line 1171, in _execute_child
    args = list(args)
TypeError: 'int' object is not iterable

任何人都知道如何解决此错误?

python logging subprocess python-3.5
1个回答
1
投票

subprocess.Popen函数接收“程序参数序列或单个字符串”作为其args参数。

你在args论证中传递的是os.system()调用的输出,根据documentation,它是“过程的退出状态”,因此是int数。而不是在cmd变量中,您应该直接传递文件/tests_run.py的字符串(或其他迭代器)。如果您希望此路径相对于当前项目,则可以使用os.path模块。

© www.soinside.com 2019 - 2024. All rights reserved.