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 <noreply@anthropic.com>
This commit is contained in:
@@ -205,13 +205,17 @@ class LunarCalendar:
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def save_json(self, filename: str | None = None):
|
def save_json(self, filename: str | None = None, output_dir: str = "lunar_json"):
|
||||||
"""將該年日曆輸出為 JSON 檔"""
|
"""將該年日曆輸出為 JSON 檔"""
|
||||||
|
import os
|
||||||
|
|
||||||
data = self.build_calendar()
|
data = self.build_calendar()
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
fname = filename or f"{self.year}.json"
|
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)
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||||
print(f"完成!已輸出:{fname}")
|
print(f"完成!已輸出:{filepath}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user