从python代码运行可执行文件

时间:2015-02-05 11:52:12

标签: python system exe

我正在尝试使用colordescriptor.exe从图像数据库创建一个代码簿。代码附带一个python scirpt,实际上是创建一个代码簿的scirpt。在用法示例中,它说我必须运行以下命令来控制:python exampleCreateCodebook.py trainimages.txt outputFilename,其中trainimages.txt是数据集图像列表,outputFilename是输出码本的名称。当我尝试运行命令(从.exe所在的目录)时,我收到以下错误:

Traceback (most recent call last):
File "exampleCreateCodebook.py", line 112, in <module>
sys.exit(main())
File "exampleCreateCodebook.py", line 109, in main
return process(options, args)
File "exampleCreateCodebook.py", line 80, in process
raise Exception("Error when executing '%s': command returned error" % cmdLine)
Exception: Error when executing 'colorDescriptor.exe C:\sift\fashion1\00010big.jpg --keepLimited 30 --outputFormat binary --output c:\users\user\appdata\local\temp\tmp9qzldd --detector densesampling --ds_spacing 6 --ds_scales 1.2 --descriptor opponentsift': command returned error

如何正确运行exampleCreateCodebook?我是否有机会因os.system功能而遇到问题? scirpt文件是following。我注意到我的程序在第80行失败,因为returnCode等于-1。因此os.system(cmdLine)似乎没有正常运行。我试图用returnCode =subprocess.call(['cmd', '/k', cmdLine] )更改上面的命令。当我这样做时,scirpt在for循环中只迭代一次,并在subprocess.call之后停止。子进程是否负责停止for循环?

for inputImage in inputImages:
    cmdLine = "%s %s --keepLimited %d --outputFormat binary --output %s %s" % (binarySoftware, inputImage, keepLimited, tempFilename, extractionConfig)
    print cmdLine
    subprocess.call(['cmd', '/k', cmdLine] )
    print 'ole'

列表inputImages包含1000张图像。但是,for循环在第一次迭代后,在第一次调用子进程时停止。

1 个答案:

答案 0 :(得分:1)

您显示的输出是否正确?它看起来不像是逐字复制的。

查看example Python code,无法在第80行处引发异常,以便执行第82行。

如果由某个奇迹执行第82行,如果IOError: [Errno 22] Invalid argument生成的输出文件长度小于4个字节,则会发生colorDescriptor.exe。当colorDescriptor.exe在运行期间失败时可能就是这种情况。 DescriptorIO.py的第84-85行:

identify = f.read(4)
f.seek(-4, os.SEEK_CUR)

看起来像是一个假设文件至少有4个字节的错误。如果不seek()IOError将会失败。

您需要弄清楚colorDescriptor.exe失败的原因。尝试手动运行程序,您可能会发现问题的根源:

colorDescriptor.exe C:\sift\fashion1\00010big.jpg --keepLimited 30 --outputFormat binary --output    c:\users\user\appdata\local\temp\tmpywsquy --detector densesampling --ds_spacing 6 --ds_scales 1.2 --descriptor opponentsift

N.B。我修改了上面的命令删除了-- keepLimited中的空格,因为这就是代码应该生成的内容。如果它真的在命令中生成空格,那可能就是问题。