4

嗨,我对 Python 很陌生,我正在尝试使用 subprocess.call 从另一个 Python 脚本调用子进程。但我的论点是变量名。那么,我应该使用 subprocess.call 还是 subprocess.popen ?

我想从另一个 python 脚本执行以下命令:

python npp.python -i fname -o fname+"out" -l fname+"log" -e excplist -i ignorelist 

那么,我应该做

subprocess.Popen(['python', 'npp.python', '-i', fname , 'o', fname+"out", '-l', fname+"log", '-e', excplist,'-i',ignrlist]).communicate()

我无法通过这样做来调用其他程序。关于我做错了什么的任何建议?

4

1 回答 1

0

P仅供参考。执行此类操作的一个非常简单的方法是预先定义命令,然后将其转换为参数列表。

command = "python npp.python -i {file} -o {file}.out -l {file}.log -e {excep} -i {ignore}".format(file=pipe.quote(fname), excep=exceptlist, ignore=ignorelist)

subprocess.call(shlex.split(command)) # shlex.split is safer for shell commands than the usual split
# or popen if the return code isn't needed
subprocess.Popen(shlex.split(command))

这样在以列表形式编写命令时更难出错。

于 2013-06-27T08:52:35.437 回答