Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Write a Dictionary to a File in Python

  • 31 Oct, 2025
  • Com 0
How to write a dictionary to a file in Python

To write a dictionary to a file in Python, you can serialize the data to json using json.dump() or json.dumps() method or serialize it to a string by converting it into a string using str(), then writing it using write().

Method 1: Using json.dump()

The json.dump() is a built-in function that directly writes a dictionary as a JSON object to a file. After you write it, you can open the file and read it easily or manipulate it as needed.

import json

shares = {'Name': "Netweb Tech", 'Price': 4051}

# writing dictionary to a file as JSON
with open('data.json', 'w') as f:
    json.dump(shares, f)
    print("Data has been written to data.json file")

# Output: Data has been written to data.json file

Write a dictionary to a json file in Python

In your current working directory, you will find a new file called “data.json” with the above content.

Method 2: Using json.dumps()

The json.dumps() method does not write a dictionary to a file; instead, it converts the dictionary to a JSON string. You then need to write that JSON string to a file using the write() method. It allows us to add options like formatting and indentation.

import json

shares = {'Name': "Netweb Tech", 'Price': 4051, 'Volume': 1000, "Exchange": "NSE"}

with open('data.json', 'w') as file:
    file.write(json.dumps(shares, indent=4))

Using json.dumps() method to write a json string to a file

Method 3: Using str()

Another simple method is to use the str() method to save a dictionary as a string, then write to a file using the write() method.

shares = {'Name': "Netweb Tech", 'Price': 4051,
          'Volume': 1000, "Exchange": "NSE"}

with open('data.txt', 'w') as file:
    file.write(str(shares))

Using str() function to write a dictionary as a string to a file

That’s all!

Post Views: 3
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Save Python Dictionary to CSV File

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend