Skip to content

Instantly share code, notes, and snippets.

@tomschr
Created May 1, 2021 15:11
Show Gist options
  • Save tomschr/86a8c6f52b81e35ac4723fef8435ec43 to your computer and use it in GitHub Desktop.
Save tomschr/86a8c6f52b81e35ac4723fef8435ec43 to your computer and use it in GitHub Desktop.
Read and write JSON files with pathlib.Path
# Source
# Mastering Object-Oriented Python - Second Edition by Steven F. Lott Published by Packt Publishing, 2019
# https://learning.oreilly.com/library/view/mastering-object-oriented-python/9781789531367/c34be237-5ccd-4775-a0b0-ec1f7652f7bc.xhtml
#
from pathlib import Path
# write JSON files:
with Path("temp.json").open("w", encoding="UTF-8") as target:
json.dump(travel3, target, default=blog_j2_encode)
# read JSON files:
from pathlib import Path
with Path("some_source.json").open(encoding="UTF-8") as source:
objects = json.load(source, object_hook=blog_decode)
@tomschr
Copy link
Author

tomschr commented Mar 14, 2023

Thanks for the addition. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment