import argparse
import pathlib
import sys
from alive_progress import alive_bar
def get_raw_data(path, string):
with open(path, "rb") as f:
lines = sum(1 for _ in f)
with open(path, "r") as data:
results = []
print(f"Number of lines: {lines}\n")
with alive_bar(lines, dual_line=True, title="Analyzing") as bar:
for line in data:
if string in line:
results.append(line)
bar()
else:
bar()
continue
return results
def write_results(results, string):
current_path = pathlib.Path(__file__).parent.resolve()
with open(f"{current_path}/{string}.txt", "a") as result_file:
for result in results:
result_file.write(f"{result}")
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser(
prog="log searcher",
description="This module willl search into logs and return a file with the results")
arg_parser.add_argument("-p", "--path", help="Path to the file you want to search in")
arg_parser.add_argument("-s", "--string", help="String to search")
args = arg_parser.parse_args()
path = args.path
string = args.string
list_of_results = get_raw_data(path, string)
if list_of_results != []:
write_results(list_of_results, string)
else:
print("sorry, no matches found")
我有这段代码,但是当我运行代码时,它变得错误,我该如何修复有人可以帮助我 我试图让搜索算法变得太紧急我收到了一个与这个主题不相关的订单我必须使用这个应用程序来自动化它
Traceback (most recent call last):
File "c:\Users\USER\Desktop\twitter\new\NEW\main.py", line 41, in <module>
list_of_results = get_raw_data(path, string)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\USER\Desktop\twitter\new\NEW\main.py", line 8, in get_raw_data
with open(path, "rb") as f:
^^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not NoneType
如果有人想帮助我,请为我编辑代码,我在移动设备中并解释“TypeError:预期的 str,bytes 或 os.PathLike 对象,而不是 NoneType”
根据我的经验,Python 中的路径仅适用于路径中的
/
,而不适用于 \
。
这背后的原因是
\
是Python中的特殊转义字符。
一个快速修复方法是将路径中的
\
字符替换为 /
path = path.replace("\", "/")