简体   繁体   English

Python3.5子进程错误

[英]Python3.5 subprocess error

I am using below function to read my python scripts output line by line and save in parallel. 我正在使用以下功能逐行读取我的python脚本输出并并行保存。 But getting Traceback error in end. 但是最终导致Traceback错误。

Code: 码:

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)

When call function as : 当调用函数为:

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

I get below error: 我得到以下错误:

Error: 错误:

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

Anyone know how can i fix this error ? 有人知道我该如何解决这个错误?

The subprocess.Popen function receives a "sequence of program arguments or else a single string" as its args argument. subprocess.Popen函数接收“程序参数的序列或单个字符串”作为其args参数。

What you are passing in the args argument is the output of the os.system() call which according to the documentation is the "exit status of the process", thus an int number. 您在args参数中传递的是os.system()调用的输出,根据文档 ,该输出是“进程的退出状态”,即一个int Instead in the cmd variable you should directly pass the string (or an other iterator) of your file /tests_run.py . 而是应在cmd变量中直接传递文件/tests_run.py的字符串(或其他迭代器)。 If you want this path to be relative to your current project you can use the os.path module. 如果希望此路径相对于当前项目,则可以使用os.path模块。

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

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