![](https://neoshare.net/wp-content/uploads/2020/12/1Y4WOPAz1Mpvv9TXijnsauQ-750x420.jpeg)
9. Creating a new path and writing text
The pathlib module provides a way to create a new file and write text to it.
Let’s create a text to write to the path object.
p = pathlib.Path("id", "file1-a.json")d = json.loads(p.read_text())d
{'John': 1, 'Jane': 2}
We can now create a path and call the write_text method on it.
text = json.dumps(d)pathlib.Path("id", "newfile.json").write_text(text)
The dumps method of json module converts the content of the json file to text. We pass this text to the write_text method and it is written in the newfile.json.
We can confirm it by reading the content of the newfile.json.
pathlib.Path("id", "newfile.json").read_text()
'{"John": 1, "Jane": 2}'