user3335011
user3335011

Reputation: 67

How to pass list of files into Python subprocess

I am trying to execute a system executable on UNIX with python. I have used op.system() to do this, but really need to use subprocess.call() instead. My op.System call is below:

os.system('gmsh default.msh_timestep%06d* animation_options.geo' %(timestep));

and works fine. It calls the program gmsh and gmsh reads a series of files specified in default.msh_timestep%06d*. I then try to do the equivalent thing with subprocess, but I get errors saying that the files are not there. Below is the subprocesses call:

call(["gmsh", "default.msh_timestep%06d*" %(timestep), "animation_options.geo"],shell=True);

Does anyone know what could be going on here? I'm admittedly a Python noob, so this might be a silly question.

Upvotes: 1

Views: 277

Answers (1)

falsetru
falsetru

Reputation: 368954

Globbing is done by the shell for you. In Python, you need to do it yourself. You can use glob.glob to get file list that match the pattern:

import glob

call(["gmsh"] + glob.glob("default.msh_timestep%06d*" % (timestep,)) +
     ["animation_options.geo"])

If you want to use shell=True, pass a string isntead of a list of strings:

call("gmsh default.msh_timestep%06d* animation_options.geo" % (timestep,), shell=True)

Upvotes: 2

Related Questions