0

I am new to python, I want to call grep unix command on some file with some specific switches, but not able to get the output. Seems the * (globbing) is creating some problem

Below is working, and gives the grep output

>>> import subprocess
>>> subprocess.call('ls')
1-intro.txt  2-data-types.txt  3-numbers-integers.txt  4-strings.txt  data-chap1.txt
0
>>> cmd = ["grep", "-i", "string", "1-intro.txt" ]
>>> subprocess.call(cmd)
strings
0

Problem, when I give * as param to grep

>>> cmd = ["grep", "-i", "string", "*.txt" ]
>>> subprocess.call(cmd)
grep: *.txt: No such file or directory
2

I replaced the 1-intro.txt with *.txt. Can you please let me know, what am I doing wrong? Why isn't the 2nd part working?

Note: Python 2.4.3

2
  • See also Python Subprocess Grep Commented Apr 1, 2014 at 12:38
  • btw, you could use glob.glob(), os.path.expanduser(), os.listdir('.'), etc instead of shell=True Commented Apr 1, 2014 at 12:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.