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
如何解决上述追溯?
答案 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)