Shell expansion in Python subprocess [duplicate]

倖福魔咒の 提交于 2019-12-12 15:52:17

问题


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?


回答1:


Use the glob package:

import subprocess    
from glob import glob
subprocess.call(["mock"] + glob("*.src.rpm"))



回答2:


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)


来源:https://stackoverflow.com/questions/14482135/shell-expansion-in-python-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!