fx.*_*fx. 16 python linux executable system-calls
我需要从我的Python脚本中执行这个脚本.
可能吗?该脚本生成一些输出,其中一些文件正在写入.我如何访问这些文件?我尝试过子进程调用函数但没有成功.
fx@fx-ubuntu:~/Documents/projects/foo$ bin/bar -c somefile.xml -d text.txt -r aString -f anotherString >output
Run Code Online (Sandbox Code Playgroud)
应用程序"bar"也引用了一些库,它还创建了除输出之外的文件"bar.xml".如何访问这些文件?只是通过使用open()?
谢谢,
编辑:
Python运行时的错误只是这一行.
$ python foo.py
bin/bar: bin/bar: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)
Pet*_*ons 29
要执行外部程序,请执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
是的,假设您的bin/bar
程序将一些其他各种文件写入磁盘,您可以正常打开它们open("path/to/output/file.txt")
.请注意,如果您不想,则无需依赖子shell将输出重定向到名为"output"的磁盘上的文件.我在这里展示如何直接读取你的python程序的输出,而不是介于两者之间的磁盘.
rz.*_*rz. 15
最简单的方法是:
import os
cmd = 'bin/bar --option --otheroption'
os.system(cmd) # returns the exit status
Run Code Online (Sandbox Code Playgroud)
您可以使用常规方式访问文件open()
.
如果您需要进行更复杂的子进程管理,那么子进程模块就是您的选择.
小智 6
用于执行unix可执行文件.我在Mac OSX中做了以下操作,它对我有用:
import os
cmd = './darknet classifier predict data/baby.jpg'
so = os.popen(cmd).read()
print so
Run Code Online (Sandbox Code Playgroud)
这里print so
输出结果.
归档时间: |
|
查看次数: |
55880 次 |
最近记录: |