I am trying to make a repeated call on the windows command line from my python code. For each fine in a directory, I need to run a command, and wait for it to finish.
try:
directoryListing = os.listdir(inputDirectory)
for infile in directoryListing:
meshlabString = #string to pass to command line
os.system(meshlabString)
except WindowsError as winErr:
print("Directory error: " + str((winErr)))
I have been reading online, and it seems the preferred way to do this is with subprocess.call(), but I cannot figure out how to run cmd.exe through subprocess.call(). It kind of works right now using os.system(), but it gets caught up trying to run a bunch of processes at once, and dies. If someone could provide me a few lines of code on how to run a command on the windows command line, and if subprocess.wait() is the best way to wait.
with subprocess, you have a few options. The easiest is call:
shlex takes the string and splits it into a list the way the shell would split it. in other words:
You could also do:
but this way is a security risk if meshlabString is untrusted. Ultimately,
subprocess.callis just a wrapper on thesubprocess.Popenclass, provided for convenience, but it has the functionality that you want here.