26 lines
952 B
Python
26 lines
952 B
Python
import os
|
|
|
|
# 获取当前文件所在目录下 materials 文件夹的绝对路径
|
|
MATERIALS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "materials")
|
|
|
|
def search_file(keyword: str) -> str:
|
|
"""
|
|
在资料文件夹中根据关键词模糊查找文件,返回文件的绝对路径。
|
|
如果找不到返回空字符串。
|
|
"""
|
|
if not os.path.exists(MATERIALS_DIR):
|
|
print(f"WARN: 资料文件夹 {MATERIALS_DIR} 不存在,已自动创建。请放入资料文件以免检索不到。")
|
|
os.makedirs(MATERIALS_DIR, exist_ok=True)
|
|
return ""
|
|
|
|
keyword_lower = keyword.lower()
|
|
|
|
# 遍历所有文件,简单粗暴的关键字包含匹配
|
|
for root, dirs, files in os.walk(MATERIALS_DIR):
|
|
for file in files:
|
|
text_to_match = file.lower()
|
|
if keyword_lower in text_to_match:
|
|
return os.path.join(root, file)
|
|
|
|
return ""
|