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 函数接收一个 "sequence of program arguments or else a single string" 作为其 args
参数。
您在 args
参数中传递的是 os.system()
调用的输出,根据 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 模块。
我正在使用以下函数逐行读取我的 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 函数接收一个 "sequence of program arguments or else a single string" 作为其 args
参数。
您在 args
参数中传递的是 os.system()
调用的输出,根据 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 模块。