python テキストデータベース
わんわん
にゃあにゃあ
# ==== テキストデータベース
一行検索(Grep式)ではない
複数ワード検索対応
ログ出力
秀丸起動 yes/no 式
再帰的??? 不明
# ==== 【Gemini】様 作成
### 改造版:ヒットしたファイルを「秀丸」で一斉に開く
# 以下のコードでは、ヒットしたファイルをすべてリストアップした後、**自動的に秀丸エディタで(タブまたは別ウィンドウとして)開きまます。**
import os
import subprocess # 外部アプリを起動するために追加
# === 設定 ===
search_folder = r"D:\■Download\txt-01"
output_file = os.path.join(search_folder, "search_result.txt")
# 秀丸エディタのフルパス(指定いただいたもの)
hidemaru_path = r"C:\Program Files (x86)\Hidemaru\Hidemaru.exe"
# === 実行 ===
print("--- AIログ・ファイル検索モード(秀丸連携版) ---")
input_words = input("検索したいワードをスペース区切りで入力してください: ")
search_words = input_words.split()
if not search_words:
print("検索ワードが入力されませんでした。")
exit()
results = []
hit_files = [] # ヒットしたファイルのパスを貯めるリスト
# フォルダ内のファイルを走査
for filename in os.listdir(search_folder):
if filename.endswith(".txt"):
filepath = os.path.join(search_folder, filename)
try:
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
content = f.read()
# 全ワードが含まれているか (AND検索)
if all(word in content for word in search_words):
results.append(f"【ヒット】: {filename}")
results.append(f" 内容一部: {content[:100].replace(os.linesep, ' ')}...")
results.append("-" * 30)
hit_files.append(filepath) # パスを保存
except Exception as e:
results.append(f"【エラー】 {filename}: {e}")
# 結果を保存
with open(output_file, "w", encoding="utf-8") as f:
f.write(f"検索ワード: {', '.join(search_words)}\n")
f.write(f"ヒット件数: {len(hit_files)} 件\n\n")
f.write("\n".join(results))
print(f"\n検索完了! {len(hit_files)}件見つかりました。")
# --- 秀丸で開く処理 ---
if hit_files:
ans = input(f"{len(hit_files)}個のファイルを秀丸で開きますか? (y/n): ")
if ans.lower() == 'y':
for path in hit_files:
# 秀丸を起動してファイルを開く
subprocess.Popen([hidemaru_path, path])
print("秀丸で起動しました。")
else:
print("ヒットしなかったため、秀丸は起動しませんでした。")
print(f"詳細は {output_file} を確認してください。")
# # ==============================