Files
llm_macro/tools/delete_error_and_empty_files.py
2025-10-30 10:32:31 +09:00

31 lines
1006 B
Python

import os
import json
dir_path = "result/gemma3-upstage/"
files_to_delete = []
for filename in os.listdir(dir_path):
if filename.endswith(".json"):
file_path = os.path.join(dir_path, filename)
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if "result" in data:
result_data = data["result"]
# Check if result is an empty dictionary or contains "Error" key
if not result_data or "Error" in result_data:
files_to_delete.append(file_path)
except (json.JSONDecodeError, KeyError) as e:
print(f"Error processing file {filename}: {e}")
for file_path in files_to_delete:
try:
os.remove(file_path)
print(f"Deleted: {os.path.basename(file_path)}")
except OSError as e:
print(f"Error deleting file {os.path.basename(file_path)}: {e}")
print(f"\nTotal deleted files: {len(files_to_delete)}")