From 0175a07cf19ca33e3a1fdab79a6ebaac9ff78b52 Mon Sep 17 00:00:00 2001 From: Timmy Date: Mon, 9 Mar 2026 14:28:16 +0800 Subject: [PATCH] Fix save_json to save files to lunar_json directory - Add output_dir parameter (default: "lunar_json") - Auto-create directory if not exists - Update output message to show full file path Co-Authored-By: Claude Sonnet 4.6 --- lunar_calendar.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lunar_calendar.py b/lunar_calendar.py index 117c8e2..c34bce3 100644 --- a/lunar_calendar.py +++ b/lunar_calendar.py @@ -205,13 +205,17 @@ class LunarCalendar: return result - def save_json(self, filename: str | None = None): + def save_json(self, filename: str | None = None, output_dir: str = "lunar_json"): """將該年日曆輸出為 JSON 檔""" + import os + data = self.build_calendar() + os.makedirs(output_dir, exist_ok=True) fname = filename or f"{self.year}.json" - with open(fname, "w", encoding="utf-8") as f: + filepath = os.path.join(output_dir, fname) + with open(filepath, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) - print(f"完成!已輸出:{fname}") + print(f"完成!已輸出:{filepath}") if __name__ == "__main__":