Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i invoke a shell command from python which includes regexp?

Tags:

python

shell

How can I invoke a shell command from python which includes regexp (i.e., cat filename*).

I wrote:

pid = subprocess.Popen(["cat", filename + "*"])

but I am getting an error

cat filename* no such file or directory.

I'd like to force the shell to treat "*" as a regexp and not as a string. Is there any way to implement this?

like image 929
curious Avatar asked Dec 06 '25 08:12

curious


1 Answers

Use an underlying shell to expand your globs:

pid = subprocess.Popen(["cat",filename+"*"], shell=True)

Alternatively, use glob.glob to expand your arguments before running the command:

import glob
pid = subprocess.Popen(["cat"] + glob.glob(filename + "*"))
like image 177
Kirk Strauser Avatar answered Dec 08 '25 21:12

Kirk Strauser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!