4

Possible Duplicate:
Python subprocess wildcard usage

Using the Python 2.6 subprocess module, I need to run a command on a src.rpm file that I am building with a previous subprocess call.

Unfortunately, I am working with spec files that are not consistent, so I only have a vague idea of what the filename of the src.rpm should look like (for instance, I know the name of the package and the extension in something named "{package}-{version}.src.rpm" but not the version).

I do know, however, that I will only have one src.rpm file in the directory that I am looking, so I can call mock with a command like

mock {options} *.src.rpm

and have it work in shell, but subprocess doesn't seem to want to accept the expansion. I've tried using (shell=True) as an argument to subprocess.call() but even if it worked I would rather avoid it.

How do I get something like

subprocess.call("mock *.src.rpm".split())

to run?

martineau
  • 119,623
  • 25
  • 170
  • 301
javanix
  • 1,270
  • 3
  • 24
  • 40
  • I think the problem you are running into is because the wildcard filename is processed by the OS before creating the command, so you would have to lookup the filenames yourself and pass them all on the command line. – Rocky Pulley Jan 23 '13 at 14:42
  • You might also want to try calling subprocess.Popen, I'm not sure if it will fix your problem or not though, this tutorial shows a sample of doing it: http://www.dreamsyssoft.com/python-scripting-tutorial/shell-tutorial.php – Rocky Pulley Jan 23 '13 at 14:44
  • You could get the list of filenames via `glob.glob('*.src.rpm')` which would return a list. Then you'd just need to prepend a `mock` on there... – mgilson Jan 23 '13 at 15:09

2 Answers2

8

Use the glob package:

import subprocess    
from glob import glob
subprocess.call(["mock"] + glob("*.src.rpm"))
Daisy Sophia Hollman
  • 6,046
  • 6
  • 24
  • 35
4

The wildcard * has to be interpreted by the SHELL. When you run subprocess.call, by default it doesn't load a shell, but you can give it shell=True as an argument:

subprocess.call("mock *.src.rpm".split(), shell=True)
skinp
  • 4,157
  • 4
  • 27
  • 20