【发布时间】:2018-06-06 20:01:29
【问题描述】:
我正在使用以下函数逐行读取我的 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
有人知道我该如何解决这个错误吗?
【问题讨论】:
-
您不需要用
os.system包装命令(顺便说一句,这是不安全的);试试myrun("./tests_run.py")(假设命令正确)。 -
我也尝试过,但没有帮助并抛出语法错误。
标签: python logging subprocess python-3.5