为什么 Python subprocess.Popen 调用 exe 文件未正常结束?解决方案?

为什么 Python subprocess.Popen 调用 exe 文件未正常结束?解决方案?

Python subprocess.Popen 调用的 exe 文件没有成功运行结束

我先写了一个 C++ 的程序

#include <bits/stdc++.h>

using namespace std;

int main() {

int n;

scanf("%d",&n);

printf("%d",n);

return 0;

}

然后成功将其编译成 exe

手动在命令行调用 "F:\\Online_Judge\\static\\code\\1" <"F:\\Online_Judge\\static\\Problems\\P1000 Online Judge 试机题目\\test_points\\1.in" >"F:\\Online_Judge\\static\\code\\1_1.out" (保证文件存在且可以正常调用,并且权限足够)

可以成功运行,将 1.in 中的数据读入并写到 1_1.out 中,用 Python 运行以下代码 subprocess.Popen(['F:\\Online_Judge\\static\\code\\1', '<F:\\Online_Judge\\static\\Problems\\P1000 Online Judge 试机题目\\test_points\\1.in', '>F:\\Online_Judge\\static\\code\\1_1.out']) 使用列表传参的方式运行命令行,但是该 exe 并没有成功运行结束,在 cmd 的 tasklist 中可看见 exe 卡住了

Python 使用 subprocess.Popen 调用 exe 文件后,exe 文件卡住没有成功运行结束,请问这是什么原因?


回答:

因为重定向的 <> 都不是参数。不能用参数的方式传递。

大概的这么写(没测):

with open("...\\i.in") as fpin, open("..\\1_1.out", "w") as fpout:

subprocess.Popen(["..\\1"], stdin=fpin, stdout=fpout);

Popen

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are None, PIPE, DEVNULL, an existing file descriptor (a positive integer), and an existing file object with a valid file descriptor. With the default settings of None, no redirection will occur. PIPE indicates that a new pipe to the child should be created. DEVNULL indicates that the special file os.devnull will be used. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

以上是 为什么 Python subprocess.Popen 调用 exe 文件未正常结束?解决方案? 的全部内容, 来源链接: utcz.com/p/939091.html

回到顶部