Aug-30-2018, 06:47 PM
modified with os.fspath looks better
import json from pathlib import Path import os def json_writer(): os.chdir(os.path.dirname(__file__)) homepath = Path('.') datapath = homepath / 'data' datapath.mkdir(exist_ok=True) jsonpath = datapath / 'json' jsonpath.mkdir(exist_ok=True) file1 = jsonpath / 'TestPath1.json' file2 = jsonpath / 'TestPath2.json' pathdict = { 'path1': os.fspath(file2), 'path2': os.fspath(file1), } with file1.open('w') as fp: json.dump(pathdict, fp) with file2.open('w') as fp: json.dump(pathdict, fp) newdict = {} with file2.open() as fp: newdict = json.load(fp) filename = Path(newdict['path1']) with filename.open() as fp: text = fp.read() print(text) if __name__ == '__main__': json_writer()still works fine:
Output:{"path1": "data/json/TestPath2.json", "path2": "data/json/TestPath1.json"}
(venv) Larz60p@linux-nnem: EncodePathlibPathJson:$