python调用可执行文件,如何从Python脚本调用可执行文件?

I need to execute this script from my Python script.

Is it possible? The script generate some outputs with some files being written. How do I access these files? I have tried with subprocess call function but without success.

fx@fx-ubuntu:~/Documents/projects/foo$ bin/bar -c somefile.xml -d text.txt -r aString -f anotherString >output

The application "bar" also references to some libraries, it also create the file "bar.xml" besides the output. How do I get access to these files? Just by using open()?

Thank you,

Edit:

The error from Python runtime is only this line.

$ python foo.py

bin/bar: bin/bar: cannot execute binary file

解决方案

For executing the external program, do this:

import subprocess

args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")

#Or just:

#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()

popen = subprocess.Popen(args, stdout=subprocess.PIPE)

popen.wait()

output = popen.stdout.read()

print output

And yes, assuming your bin/bar program wrote some other assorted files to disk, you can open them as normal with open("path/to/output/file.txt"). Note that you don't need to rely on a subshell to redirect the output to a file on disk named "output" if you don't want to. I'm showing here how to directly read the output into your python program without going to disk in between.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值