logo

... pathlib: making files



Notes

To load in the file into a dictionary we run;

import json
import pathlib

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

We can move the file to a new location by running;

path_to = pathlib.Path("data", "configuration.json")
p.rename(path_to)

Alternatively, you can also copy the file by taking the dictionary that we read in and writing that text somewhere.

text = json.dumps(d)
pathlib.Path("data", "config-1.json").write_text(text)

Feedback? See an issue? Something unclear? Feel free to mention it here.



If you want to be kept up to date, consider signing up for the newsletter.