[Python-ideas] Add pathlib.Path.write_json and pathlib.Path.read_json

Victor Stinner victor.stinner at gmail.com
Mon Mar 27 19:42:19 EDT 2017


2017-03-27 17:04 GMT+02:00 Steven D'Aprano <steve at pearwood.info>:
> Of course pathlib can already read JSON, or for that matter ReST text
> or JPG binary files. It can read anything as text or bytes, including
> JSON:
>
> some_path.write_text(json.dumps(obj))
> json.loads(some_path.read_text())

Note: You should specify the encoding:

some_path.write_text(json.dumps(obj), encoding='utf8')
json.loads(some_path.read_text(encoding='utf8'))


> I don't think it should be pathlib's responsibility to deal with the
> file format (besides text).

Right.

Victor


More information about the Python-ideas mailing list