0

I trying to find a Pattern in specific large files (GB) in subfolders

I am runnging Python code.

  1. 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...
  1. 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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

what does the ...f... stands for? - os.popen(f"grep..... I can not find it in manual
@Scripter It's an f-string, you can look it up. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.