我试图在子文件夹
中找到特定大文件(GB)的模式我正在运行Python代码
- 尝试…
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...
- 另一种方式
Bash from terminal:
grep -a 'TEXT' /folder1/FILE.txt - output OK as desired
Python代码:
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
有什么建议吗?
如何在Python中运行Bash GREP命令在二进制/文本文件中使用变量(文件路径)并将GREP输出存储到Python中的变量
您可以尝试使用os.popen(),它应该模拟并返回使用bash命令时看到的结果:
import os
FILE_PATH = "/folder1/FILE.txt"
results = os.popen(f"grep -a 'TEXT' {FILE_PATH}").read()
print(results)