繁体   English   中英

Python3.5子进程错误

[英]Python3.5 subprocess error

我正在使用以下功能逐行读取我的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

有人知道我该如何解决这个错误?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM