• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Crypto Currency
  • Technology
  • Contact
NEO Share

NEO Share

Sharing The Latest Tech News

  • Home
  • Artificial Intelligence
  • Machine Learning
  • Computers
  • Mobile
  • Crypto Currency

10 Examples to Master Python Pathlib

December 13, 2020 by systems

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}'

Filed Under: Machine Learning

Primary Sidebar

Stay Ahead: The Latest Tech News and Innovations

Cryptocurrency Market Updates: What’s Happening Now

Emerging Trends in Artificial Intelligence: What to Watch For

Top Cloud Computing Services to Secure Your Data

The Future of Mobile Technology: Recent Advancements and Predictions

Footer

  • Privacy Policy
  • Terms and Conditions

Copyright © 2025 NEO Share

Terms and Conditions - Privacy Policy