I am running a python script which calls powershell on windows. But it seems like python doesn’t wait for the powershell script to finish before moving on (or at least it seems so). Here is my code:
import numpy as np
import subprocess
filename = "out_test.txt"
subprocess.call(r"powershell.exe & '.SIMION 8.1.lnk' fly --recording-output=.out_test.txt .optimize_setup.iob")
data = np.loadtxt(filename,skiprows=1)
print(len(data))
The function called in subprocess.call
works fine when I use it in powershell itself. Basically it runs a program and saves the output in out_test.txt
However when I run from Python, I get this error: FileNotFoundError: out_test.txt not found.
So basically the python script moves to the data = np.loadtxt(filename,skiprows=1)
line, without waiting for the subprocess.call
to actually generate the file. The files will be of different lengths, so the wait time would vary so I don’t want to add by hand a wait time (e.g. using time.sleep(1)
). How can I make sure python waits for the subprocess.call
to finish? Thank you!
Online I see that python should automatically wait for subprocess.call
but it doesn’t. I tried subprocess.run
but I get the same result.