1

I have to run an executable for which input parameters are saved in a text file, say input.txt. The output is then redirected to a text file, say output.txt. In windows terminal, I use the command,

executable.exe < input.txt > output.txt

How can I do this from within a python program?

I understand that this can be achieved using os.system. But I wanted to run the same using subprocess module. I was trying something like this,

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

But, the python code executes the exe file without directing the text file to it.

1 Answers1

-1

To redirect input/output, use the stdin and stdout parameters of 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)
iBug
  • 35,554
  • 7
  • 89
  • 134