pathlib logo pathlib: pathlib

1 2 3 4 5 6 7
Notes

Let's compare the two styles.

os.path style

import os
import json

path_str = os.path.join("settings", "config.json")
with open(path_str) as f:
    d = json.loads(f.read())

pathlib style

import json
import pathlib

p = pathlib.Path("settings", "config.json")
d = json.loads(p.read_text())

It may certainly be a matter of preference, but the fact that one does not need the context manager with open() is appreciated.