Created
May 1, 2021 15:11
-
-
Save tomschr/86a8c6f52b81e35ac4723fef8435ec43 to your computer and use it in GitHub Desktop.
Read and write JSON files with pathlib.Path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
isaacimholt
commented
Mar 13, 2023
•
Thanks for the addition. 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment