python调用可执行文件并获取进程号_从python调用外部程序并获取其输出

I want to call a program (.exe), which is written in C++ and compiled, from Python. The executable takes as input two files and returns a score.

I need to do this for multiple files. So, I would like to write a small script in python which loops over multiple files, passes them to the executable and gets back the values.

Now, I have done my search and I know about SWIG and Boost::Python may be an option but I was trying to find if there is an easier way. I do not need to 'extend' the C++ program. I simply want to call it just like I would from a command line and get the returned number.

解决方案

To run an external program and get its output, use subprocess.check_output on Python 2.7+. The example from the docs:

>>> subprocess.check_output(["ls", "-l", "/dev/null"])

'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'

check_call just returns the return code of the program, not the output.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值