I have the following Python lines:
import subprocess
subprocess.Popen("egrep -r --exclude=*{.git,.svn}* \"text\" ~/directory", stdout=subprocess.PIPE, shell=True).communicate()[0]
Unfortunately, bash completely ignores the --exclude=*{.git,.svn}* flag.
I've narrowed down the problem to the curly braces. --exclude=*.git* will work through python's popen, but the moment curly braces are introduced, I'm left helpless. Any suggestions?
Note: I tried running the command using Python's command library, it produces the exact same output -- and the exact same ignored --exclude flag.
When you pass shell=True, python translates the command to /bin/sh -c <command>
(as described here). /bin/sh apparently does not support the curly braces expansion. You can try the following instead:
import subprocess
subprocess.Popen(["/bin/bash", "-c", "egrep -r --exclude=*{.git,.svn}* \"text\" ~/directory"], stdout=subprocess.PIPE).communicate()[0]
I would guess that it might be shell escaping?
It might be best to do your own splitting up of the arguments, and avoid the shell entirely?
import subprocess
subprocess.Popen(["egrep","-r","--exclude=*{.git,.svn}*","text","~/directory"], stdout=subprocess.PIPE).communicate()[0]
NB: You might have to expand ~
, I'm not sure.
Or if bash is supposed to be expanding the braces then you could do it in python:
excludes = ['.git','.svn']
command = ['egrep','-r']
for e in excludes:
command.append('--exclude=*%s*'%e)
command += ["text","~/directory"]
subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With