Skip to content

Instantly share code, notes, and snippets.

@tomschr
Created May 1, 2021 15:11
Show Gist options
  • Select an option

  • Save tomschr/86a8c6f52b81e35ac4723fef8435ec43 to your computer and use it in GitHub Desktop.

Select an option

Save tomschr/86a8c6f52b81e35ac4723fef8435ec43 to your computer and use it in GitHub Desktop.

Revisions

  1. tomschr created this gist May 1, 2021.
    16 changes: 16 additions & 0 deletions pathlib_and_json.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # 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)