使用子流程时出现TypeError

时间:2020-03-28 14:48:10

标签: python python-3.x subprocess typeerror

我正在使用子过程将另一个python文件的输出捕获到当前文件中。这是我的代码-

    import subprocess
    a= subprocess.run('python3 try1.py', capture_output=True,shell=True)

但是当我运行代码时,出现错误-

<pre>Traceback (most recent call last):
  File &quot;test2.py&quot;, line 4, in &lt;module&gt;
    c1= subprocess.run(&apos;python3 test2.py&apos;, capture_output=True,shell=True)
  File &quot;/usr/lib/python3.6/subprocess.py&quot;, line 423, in run
    with Popen(*popenargs, **kwargs) as process:
TypeError: __init__() got an unexpected keyword argument &apos;capture_output&apos;
</pre>

我正在运行Python 3.6.8。 另外,我的电脑上没有名为subprocess.py的文件。它曾经,但是我删除了它。 感谢所有帮助!

1 个答案:

答案 0 :(得分:3)

您正在使用的Python 3.6中不存在capture_output参数。因此,错误。您可以改用它:

subprocess.check_output(['python3', 'try1.py'])