Issue
I trying to find a Pattern in specific large files (GB) in subfolders
I am runnging Python code.
- tried....
FILE_PATH=/folder1/FILE.txt - OK, absolute path with open (FILE_PATH, "r") as FILE: for index, x in enumerate(FILE): if re.findall(".*TEXT.*", x): ...takes too much time...
- another way
in Bash from terminal:
grep -a 'TEXT' /folder1/FILE.txt - output OK as desired
Python code:
FILE_PATH=/folder1/FILE.txt - OK, absolute path STATUS=(subprocess.check_output("grep -a \'TEXT\' " + str(FILE_PATH.encode()), shell=True)).rstrip('\n') I get this output in terminal ...: Command 'grep -a 'TEXT' b'/folder1/FILE.txt'' returned non-zero status 2
Any advice, please?
How to run Bash GREP command in Python on both binary/text file with variables (File path) ang store grep output into Variable in Python
Solution
You could try to use os.popen(), which should mimic and return the result you see when using the bash command:
import os
FILE_PATH = "/folder1/FILE.txt"
results = os.popen(f"grep -a 'TEXT' {FILE_PATH}").read()
print(results)
Answered By - user56700 Answer Checked By - Robin (WPSolving Admin)