48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Count words in all text files in the scratch directory and output JSON report."""
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
|
|
SCRATCH_DIR = "/root/.hermes/profiles/spark/cache/scratch"
|
|
OUTPUT_FILE = "/root/.hermes/wizard-outputs/spark/word_count_report.json"
|
|
|
|
def count_words_in_file(filepath):
|
|
"""Count words in a file, return 0 if unreadable."""
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
|
|
content = f.read()
|
|
return len(content.split())
|
|
except Exception:
|
|
return 0
|
|
|
|
def main():
|
|
results = []
|
|
total_words = 0
|
|
|
|
scratch_path = Path(SCRATCH_DIR)
|
|
if scratch_path.exists():
|
|
for filepath in sorted(scratch_path.iterdir()):
|
|
if filepath.is_file():
|
|
word_count = count_words_in_file(filepath)
|
|
total_words += word_count
|
|
results.append({
|
|
"file": filepath.name,
|
|
"word_count": word_count
|
|
})
|
|
|
|
report = {
|
|
"scratch_directory": SCRATCH_DIR,
|
|
"files_processed": len(results),
|
|
"total_words": total_words,
|
|
"files": results
|
|
}
|
|
|
|
with open(OUTPUT_FILE, 'w') as f:
|
|
json.dump(report, f, indent=2)
|
|
|
|
print(f"Word count report written to {OUTPUT_FILE}")
|
|
print(f"Files: {len(results)}, Total words: {total_words}")
|
|
|
|
if __name__ == "__main__":
|
|
main() |