I have seen a lot of solutions, but I am not seeing one that works I am trying to grep every file in a directory in Python for a specific string, count the number of lines that the grep returns, and record this in python. Here's what I have tried most recently:
for f in try_files:
print("trying %s"%f)
s = subprocess.Popen("grep -r '%s' ../dir/*"%f)
print(s)
I am getting this error:
trying accept_button_off_transparent.png
Traceback (most recent call last):
File "findImages.py", line 17, in <module>
s = subprocess.Popen("grep -r %s '../dir/*'"%f)
File "/Users/agsrn/anaconda3/lib/python3.5/subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "/Users/agsrn/anaconda3/lib/python3.5/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: "grep -r accept_button_off_transparent.png '../dir/*'"
Agsrn-MacBook-Pro:images agsrn$ emacs findImages.py
Agsrn-MacBook-Pro:images agsrn$ python findImages.py
['accept_button_off_transparent.png', 'accept_button_on.png', 'accept_button_on_food.png', 'accept_button_on_transparent.png']
trying accept_button_off_transparent.png
Traceback (most recent call last):
File "findImages.py", line 17, in <module>
s = subprocess.Popen("grep -r '%s' ../dir/*"%f)
File "/Users/agsrn/anaconda3/lib/python3.5/subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "/Users/agsrn/anaconda3/lib/python3.5/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
Ultimately I want to execute this query from within Python:
grep -r "filename" ../dir/* | wc -l
...And get that line count back as a # I can use for other logic. What's the best way to do this?
To be clear, my ultimate goal is to count how many times a particular string is mentioned by any/all files in a directory for a list of a bunch of strings. I am looking for strings inside files, not just file names. I suspect grep is a much faster solution to do this than Python, but it's inside a larger Python routine, hence the proposed hybrid solution.