【发布时间】:2012-05-07 23:30:10
【问题描述】:
我正在使用 Python 的 subprocess 模块来启动另一个程序。该程序需要一个参数“-c{0-7}”。
this_dir = os.path.dirname(os.path.abspath(__file__))
cmd = [os.path.join(this_dir,'foobar'),'-c%d' % channel]
print "Starting process: %s" % str(cmd)
Proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
在 C++ 程序中,我正在检查传入的参数:
for (int i = 0; i < argc; i++)
{
cerr << i << " " << argv[i] << endl;
}
cerr << "" << endl;
这是我运行 python 脚本时的输出:
user@home:~/embedded_pqa/saleae$ ./foobar.py -c3
Starting process: ['/home/user/code/foobar', '-c3']
0 /home/user/code/foobar
很明显,参数“-c3”没有传递给子进程。有什么想法吗?
【问题讨论】:
-
像
strace或truss这样的工具是正确的,可以查看实际传递给execve系统调用的内容。另外,shell=True是邪恶的;如果您想控制参数的传递方式,请不要使用它。
标签: python subprocess popen