我如何从Python执行此终端命令?

Prashanth Balakrishna

我必须运行一个可执行文件,其输入参数保存在文本文件中,例如input.txt。然后将输出重定向到文本文件,例如output.txt。在Windows终端中,我使用以下命令,

executable.exe < input.txt > output.txt

如何在python程序中执行此操作?

我知道可以使用os.system来实现。但是我想使用子进程模块运行相同的代码。我在尝试这样的事情

input_path = '<'+input+'>'
temp = subprocess.call([exe_path, input_path, 'out.out'])

但是,python代码执行了exe文件,而没有将文本文件定向到该文件。

iBug

要重定向输入/输出,请使用stdin和的stdout参数call

with open(input_path, "r") as input_fd, open("out.out", "w") as output_fd:
    temp = subprocess.call([exe_path], stdin=input_fd, stdout=output_fd)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章