Your code has several issues. The immediate answer to what you seem to be asking was given in a comment, but there are more things to fix here.
If you want to pass in a variable instead of a static string, you have to use some sort of string interpolation.
grep
already knows how to report how many lines matched; use grep -c
. Or just ask Python to count the number of output lines. Trimming off the pipe to wc -l
allows you to also avoid invoking a shell, which is a good thing; see also Actual meaning of shell=True
in subprocess.
grep
already knows how to search for multiple expressions. Try passing in the whole list as an input file with grep -f -
.
import subprocess as sp
out = sp.check_output(
["grep", "-r", "-f", "-", "."],
input="\n".join(python_files), text=True)
print(len(out.splitlines()))
If you want to speed up your processing and the patterns are all static strings, try also adding the -F
option to grep
.
Of course, all of this is relatively easy to do natively in Python, too. You should easily be able to find examples with os.walk()
.