首页 > 解决方案 > 使用子进程时出现类型错误

问题描述

我正在使用子进程将另一个 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。此外,我的 PC 上不存在名为 subprocess.py 的文件。以前有,不过我删了 感谢大家的帮助!

标签: pythonpython-3.xsubprocesstypeerror

解决方案


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

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

推荐阅读