简体   繁体   English

从python脚本调用exe

[英]Call exe from python script

I am trying to read files from folder and to run the colorDescriptor.exe which is in the same directory with .py file. 我试图从文件夹中读取文件并运行与.py文件位于同一目录中的colorDescriptor.exe Actually I want, every time that it reads a file to calculate the colorDescriptor. 实际上,我想每次读取文件来计算colorDescriptor。

My code is the below: 我的代码如下:

import os

from os import listdir
from os.path import isfile, join
mypath1 = "/clothes/"
mypath2 = "/i386-win-vc/"
onlyfiles = [ f for f in listdir(mypath1) if isfile(join(mypath1,f)) ]
image = mypath1+f
os.popen("colorDescriptor image --detector harrislaplace --descriptor sift --output 
onlyfiles.txt ")
print image

From the terminal, the syntax to use colorDescriptor.exe is for example: 在终端上,使用colorDescriptor.exe的语法例如:

colorDescriptor image.jpg --detector harrislaplace --descriptor sift --output onlyfiles.txt

I am receiving as an error: 我收到错误消息:

Tue04 10:53:30,248 - [Impala.Persistency.FileSystem ] Unable to find image in path 
Tue04 10:53:30,248 - [Impala.Core.Array.ReadFile ] Don't know how to read 
Tue04 10:53:30,248 - [Sandbox.koen.mainColorDescriptor ] [ERROR] Could not read input   
file: is it really a valid image? image

After change it with the proposed code: 用建议的代码对其进行更改后:

import os

from os import listdir
from os.path import isfile, join
mypath1 = "C:/Documents and Settings/Desktop/clothes/"
mypath2 = "C:/Documents and Settings/My  
Documents/colordescriptors40/i386-win-vc/"
onlyfiles = [ f for f in listdir(mypath1) if isfile(join(mypath1,f)) ]
image = mypath1+f
print  image
pattern = "colorDescriptor %s --detector harrislaplace --descriptor sift --output 
onlyfiles.txt" 
os.system(pattern % image)

I am receiving now the below: 我现在收到以下信息:

 Tue04 11:06:45,091 ERROR [Impala.Persistency.FileSystem ] Unable to find C:/Documents 
 in 
 path 
 Tue04 11:06:45,091 INFO  [Impala.Persistency.FileSystem ]     
 Tue04 11:06:45,091 ERROR [Impala.Core.Array.ReadFile ] Don't know how to read 
 Tue04 11:06:45,091 ERROR [Sandbox.koen.mainColorDescriptor ] [ERROR] Could not read input 

file: is it really a valid image? 文件:这真的是有效的图像吗? C:/Documents [Finished in 0.1s] C:/文档[以0.1秒完成]

The problem is that you are not using the values you generate in your command. 问题是您没有使用在命令中生成的值。 You need to use glob.glob to get a list of the image, (probably '*.jpg'), files in the directory and then for each create a new outfile.text name and command something like: 您需要使用glob.glob来获取目录中图像的列表(可能是* .jpg),然后为每个文件创建一个新的outfile.text名称并执行以下命令:

    cmd = "colorDescriptor %s --detector harrislaplace --descriptor sift --output %s.txt " % (imagepath, imagepath)
    os.popen(cmd)

The error message makes it clear: Your sample code runs colorDescriptor on the file image in the current directory. 该错误消息使之很清楚:您的示例代码在当前目录中的文件image上运行colorDescriptor From the code context, though, we can see that image is a variable containing the path and real filename. 但是,从代码上下文中,我们可以看到image是一个包含路径和实际文件名的变量。 So do it like this: 所以这样做:

pattern = "colorDescriptor %s --detector harrislaplace --descriptor sift --output onlyfiles.txt" 
os.popen(pattern % image)

Edit: To use the same variable in for the output filename as well, the best way is to switch to python's new named syntax. 编辑:要在输出文件名中也使用相同的变量,最好的方法是切换到python的新命名语法。 Here's an example that puts all output files in the directory from which you run the script, rather than in the same directory as each file (I trust you see how to change this if it's not what you want). 这是一个示例,该示例将所有输出文件放在运行脚本的目录中,而不是与每个文件位于同一目录中(我相信您会看到如何更改它,如果不是所需的话)。

pattern = "colorDescriptor {path}/{file} --output {file}.txt --detector harrislaplace --descriptor sift" 
os.popen( pattern.format(path=mypath1, file=f) )

I rearranged the order of arguments for better visibility-- I assume it makes no difference. 我重新排列了参数的顺序以提高可见性-我认为这没有什么区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM