简体   繁体   English

在python中运行exe文件 - 不工作

[英]Running an exe file in python - not working

I have a crazy problem.我有一个疯狂的问题。 I have a cmd to run an exe file and it executes with no errors.我有一个 cmd 来运行一个 exe 文件,它执行时没有错误。 The cmd in command prompt is命令提示符中的 cmd 是

E:\project\cpp\myfirst.exe

I have to call this exe file within my python script.我必须在我的 python 脚本中调用这个 exe 文件。 I use subprocess.call.我使用 subprocess.call。 But I get an error.但我得到一个错误。 The code and error is as follows代码和错误如下

import subprocess
subprocess.call('E:\\project\\cpp\\myfirst.exe')

The error i get is我得到的错误是

ERROR: Could not open myfirst setup file
1

I couldnt find the solution.我找不到解决方案。 I also tried os.system call.我也试过 os.system 调用。 But still the same error.但仍然是同样的错误。 can you guys help me.你们能帮帮我吗?

NOTE: the exe file is generated from a cpp code注意:exe 文件是从 cpp 代码生成的

thanks谢谢

The program seems to be seeking for some configuration file in the working directory, which is not always the same as the one where the executable is.该程序似乎正在工作目录中寻找一些配置文件,该文件并不总是与可执行文件所在的目录相同。 Try instead:请尝试:

import subprocess
subprocess.call('myfirst.exe', cwd=r'E:\project\cpp')

If you have written myfirst.exe yourself, consider changing the lookup logic so that it checks the executable's own directory.如果您自己编写了myfirst.exe ,请考虑更改查找逻辑,以便检查可执行文件自己的目录。

Under Linux I have always found the popen mechanism to be more reliable.在 Linux 下我一直发现popen机制更可靠。

from subprocess import Popen, PIPE process = Popen(['swfdump',
'/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE) stdout, stderr =process.communicate()

Answer taken from答案取自

How to use subprocess popen Python 如何使用子进程popen Python

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

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