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

时间:2020-12-16 20:29:18

I need to execute this script from my Python script.

我需要从我的Python脚本执行这个脚本。

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()?

应用程序“bar”还引用了一些库,它还创建了文件“bar”。除了输出xml”。如何访问这些文件?通过使用open()?

Thank you,

谢谢你!

Edit:

编辑:

The error from Python runtime is only this line.

Python运行时的错误仅仅是这一行。

$ python foo.py
bin/bar: bin/bar: cannot execute binary file

3 个解决方案

#1


24  

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.

是的,假设您的bin/bar程序向磁盘编写了一些其他类型的文件,您可以使用open(“path/to/output/file.txt”)打开它们。注意,如果不需要,不需要依赖子shell将输出重定向到名为“output”的磁盘文件。我在这里展示的是如何直接将输出读取到您的python程序中,而不需要在中间进行磁盘操作。

#2


11  

The simplest way is:

最简单的方法是:

import os
cmd = 'bin/bar --option --otheroption'
os.system(cmd) # returns the exit status

You access the files in the usual way, by using open().

使用open()以通常的方式访问文件。

If you need to do more complicated subprocess management then the subprocess module is the way to go.

如果您需要进行更复杂的子流程管理,那么子流程模块就是最佳选择。

#3


3  

For executing a unix executable file. I did the following in my Mac OSX and it worked for me:

用于执行unix可执行文件。我在Mac OSX上做了如下操作,它对我很有用:

import os
cmd = './darknet classifier predict data/baby.jpg'
so = os.popen(cmd).read()
print so

Here print so outputs the result.

这里打印输出结果。

#1


24  

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.

是的,假设您的bin/bar程序向磁盘编写了一些其他类型的文件,您可以使用open(“path/to/output/file.txt”)打开它们。注意,如果不需要,不需要依赖子shell将输出重定向到名为“output”的磁盘文件。我在这里展示的是如何直接将输出读取到您的python程序中,而不需要在中间进行磁盘操作。

#2


11  

The simplest way is:

最简单的方法是:

import os
cmd = 'bin/bar --option --otheroption'
os.system(cmd) # returns the exit status

You access the files in the usual way, by using open().

使用open()以通常的方式访问文件。

If you need to do more complicated subprocess management then the subprocess module is the way to go.

如果您需要进行更复杂的子流程管理,那么子流程模块就是最佳选择。

#3


3  

For executing a unix executable file. I did the following in my Mac OSX and it worked for me:

用于执行unix可执行文件。我在Mac OSX上做了如下操作,它对我很有用:

import os
cmd = './darknet classifier predict data/baby.jpg'
so = os.popen(cmd).read()
print so

Here print so outputs the result.

这里打印输出结果。