2

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?

2
  • What file do you want cat to open? Could you give an example? Commented Oct 21, 2011 at 15:32
  • i have a folder where i have splitted a file with name filaname in : filaneme00,filaename01,...filenamen. and i want afterwards to compose with this: cat filename*>filename_original_version Commented Oct 21, 2011 at 15:37

2 Answers 2

6

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 + "*"))
Sign up to request clarification or add additional context in comments.

Comments

4

FIRST it's not regular expression it's shell expansion (glob) that you seem to want to execute. Which means that you should invoke /bin/sh to execute cat. The following should do the trick:

pid = subprocess.Popen(["/bin/sh", "-c", "cat", filename + "*"]);

Now /bin/sh will actually perform the asterisk expansion for you and you'll get the desired result.

Or you can use globbing library to do globbing expansions yourself (i.e. do the work that shell does):

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

glob.glob() returns the array of files that match the globbing.

4 Comments

When i am using the glob.glob ( filename + "*" ) the files are not combined in alphabetical order as when you open the shell and type cat filename* . When i invoke a new shell i am getting this: cat: -: Input/output error. The same is happening with @Kirk's answers
Yes, glob uses the order the files are stored in the directory, which is not alphabetical. You could put sorted(...) around the glob.glob() call.
Ahmed, the shell=True argument to Popen() accomplishes the same thing.
Ah :-) Thanks for that... my python 2.x is weak. I started using it from 3.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.