使用subprocess.call时的回溯

时间:2015-08-04 11:52:16

标签: python python-2.7 subprocess

import sys
import subprocess
arg1= sys.argv[1]
subprocess.call("inversion_remover.py",arg1)
subprocess.call("test3.py")
subprocess.call("test4.py")

我得到以下追溯

Traceback (most recent call last):
  File "parent.py", line 4, in <module>
    subprocess.call("inversion_remover.py",arg1)
  File "/usr/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 659, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

如何解决上述追溯?

1 个答案:

答案 0 :(得分:3)

您需要将命令作为列表传递:

subprocess.call(["inversion_remover.py", arg1])
subprocess.call(["test3.py"])
subprocess.call(["test4.py"])

否则您的arg1值将作为Popen()参数传递给基础bufsize对象。

请注意,必须在路径上找到脚本。如果要从本地目录执行这些文件,请在路径前加./,或者扩展PATH环境变量以包含当前工作目录:

subprocess.call(["./inversion_remover.py", arg1])
subprocess.call(["./test3.py"])
subprocess.call(["./test4.py"])

import os

env = os.environ.copy()
env['PATH'] = os.pathsep.join(['.', env['PATH']])

subprocess.call(["inversion_remover.py", arg1], env=env)
subprocess.call(["test3.py"], env=env)
subprocess.call(["test4.py"], env=env)