简体   繁体   中英

Python3.5 subprocess error

I am using below function to read my python scripts output line by line and save in parallel. But getting Traceback error in end.

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.

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. Instead in the cmd variable you should directly pass the string (or an other iterator) of your file /tests_run.py . If you want this path to be relative to your current project you can use the os.path module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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