【发布时间】:2022-11-10 18:04:24
【问题描述】:
我试图在子文件夹中的特定大文件 (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 中:
grep -a 'TEXT' /folder1/FILE.txt - output OK as desiredPython代码:
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 中的变量中
【问题讨论】:
标签: python grep binary subprocess encode