Use python subprocess.call to run a .exe in another directory

50 Views Asked by At

I have run into a wall trying to use subprocess.run().

In windows, outside Visual Sudio, I open a command window and get:

c:\users>

Then I change directory

c:\users>cd c:\xfoil

Then I run the xfoil program

c:\xfoil\xfoil.exe < input_file.txt

xfoil runs and generates output text files.

I am trying to get this embedded in my code so I don't have to manually generate and import the xfoil input/output text files.

Here is an example of code I tried that didn't work.

import subprocess

xfoil_output = subprocess.run(r'c:\xfoil\xfoil.exe < input_file.txt')

It runs, but just runs forever, it doesn't give any errors, or create any output files. (Xfoil should generate those files in the xfoil directory, don't know or care what value the variable xfoil_output has). I have to stop debugging to end the program.

Found lots of examples here and elsewhere but can't make it work.

1

There are 1 best solutions below

12
jepozdemir On

You should read the contents of the input file first, then should use subprocess.run() to execute XFOIL by providing the input content directly, like below:

import subprocess
import os

os.chdir(r'c:\xfoil')

with open(r'input_file.txt', 'rb') as f:
    input_content = f.read()

xfoil_process = subprocess.run(['xfoil.exe'], input=input_content, capture_output=True)
xfoil_output = xfoil_process.stdout