Is there any way to pass variable in subprocess? This didn't work.
subprocess.check_output(["cat test.txt | grep %s"], shell=True) %(variable)
Is there any way to pass variable in subprocess? This didn't work.
subprocess.check_output(["cat test.txt | grep %s"], shell=True) %(variable)
You can use str.format
:
"cat test.txt | grep {}".format(variable)
Or using old style formatting put the variable directly after.
"cat test.txt | grep %s"%variable
Your list is also redundant when using shell=True:
subprocess.check_output("cat fable.txt | grep %s" %variable, shell=True)
If you were using a list of args without shell = True, you could use Popen:
from subprocess import Popen,PIPE
p1 = Popen(["cat","text.txt"], stdout=PIPE)
p2 = Popen(["grep", variable], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
out,err = p2.communicate()
p1.wait()
print(out)