How to read Dictionary from File in Python?
Last Updated :
27 May, 2025
In Python, reading a dictionary from a file involves retrieving stored data and converting it back into a dictionary format. Depending on how the dictionary was saved—whether as text, JSON, or binary-different methods can be used to read and reconstruct the dictionary for further use in your program. In the following examples, the input file used is dictionary.txt.

Using pickle
Pickle is a quick way to save and load Python objects by packing them into a binary file. It preserves the exact data structure, but the file isn’t human-readable. Use pickle for fast, internal Python data storage.
Python
import pickle
d = {"Name": "John", "Age": 21, "Id": 28}
with open("dictionary.pkl", "wb") as file:
pickle.dump(d, file)
with open("dictionary.pkl", "rb") as file:
d = pickle.load(file)
print(type(d),d)
Output
<class 'dict'> {'Name': 'John', 'Age': 21, 'Id': 28}
Explanation: pickle.dump() write this dictionary d into a file named dictionary.pkl in binary format (wb means write binary). Later, the file is opened again in read-binary mode (rb) and pickle.load() reads the data from the file and converts it back into a dictionary.
Using json module
JSON is a popular, readable text format for saving data, compatible with many programming languages. It’s ideal for sharing dictionaries, but values must be simple types like strings or numbers.
Python
import json
with open('dictionary.json', 'r') as file:
d = json.load(file)
print(type(d),d)
Output
<class 'dict'> {'Name': 'John', 'Age': 21, 'Id': 28}
Explanation: json.load() reads data from the file dictionary.json, which is in JSON format and converts it back into a Python dictionary. The file is opened in read mode ('r').
Using ast.literal_eval()
Sometimes dictionaries are saved as strings resembling Python code. ast.literal_eval() safely converts these strings back to dictionaries by only parsing basic data types, making it safer than eval().
Python
import ast
with open('dictionary.txt', 'r') as file:
data = file.read()
d = ast.literal_eval(data)
print(type(d),d)
Output
<class 'dict'> {'Name': 'John', 'Age': 21, 'Id': 28}
Explanation: dictionary.txt is opened in read mode and its contents (a string that looks like a Python dictionary) are read into data. ast.literal_eval() safely converts this string back into a real Python dictionary.
Using eval()
eval() runs a string as Python code, converting a dictionary saved as a string back into a real dictionary. But it’s risky if the file isn’t trusted, since it can execute harmful code. Use only when you’re completely sure the file is safe.
Python
with open('dictionary.txt', 'r') as file:
data = file.read()
d = eval(data)
print(type(d),d)
Output
<class 'dict'> {'Name': 'John', 'Age': 21, 'Id': 28}
Explanation: dictionary.txt is opened in read mode and its contents are read as a string into data. eval(data) then converts this string into a Python dictionary by executing it as code.
Similar Reads
How to Read from a File in Python
Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently.Example File: geeks.txtHello World Hello GeeksforGe
5 min read
How To Read .Data Files In Python?
Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary w
4 min read
Python - Read file from sibling directory
In this article, we will discuss the method to read files from the sibling directory in Python. First, create two folders in a root folder, and one folder will contain the python file and the other will contain the file which is to be read. Below is the dictionary tree: Directory Tree: root : | |__S
3 min read
How to read specific lines from a File in Python?
Text files are composed of plain text content. Text files are also known as flat files or plain files. Python provides easy support to read and access the content within the file. Text files are first opened and then the content is accessed from it in the order of lines. By default, the line numbers
3 min read
Return Dictionary from a Function in Python
Returning a dictionary from a function allows us to bundle multiple related data elements and pass them back easily. In this article, we will explore different ways to return dictionaries from functions in Python.The simplest approach to returning a dictionary from a function is to construct it dire
3 min read
How to save a Python Dictionary to a CSV File?
CSV (Comma-Separated Values) files are a popular format for storing tabular data in a simple, text-based format. They are widely used for data exchange between applications such as Microsoft Excel, Google Sheets and databases. In this article, we will explore different ways to save a Python dictiona
4 min read
Write a dictionary to a file in Python
A dictionary store data using key-value pairs. Our task is that we need to save a dictionary to a file so that we can use it later, even after the program is closed. However, a dictionary cannot be directly written to a file. It must first be changed into a format that a file can store and read late
3 min read
Reading Rows from a CSV File in Python
CSV stands for Comma Separated Values. This file format is a delimited text file that uses a comma as a delimiter to separate the text present in it. Or in other words, CSV files are used to store data in tabular form. As per the name suggested, this file contains the data which is separated by a co
5 min read
Get Key from Value in Dictionary - Python
The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value. Using next() with a Generator ExpressionThis is the most efficient when we only need the first matching key. This meth
5 min read
How to Fix - KeyError in Python â How to Fix Dictionary Error
Python is a versatile and powerful programming language known for its simplicity and readability. However, like any other language, it comes with its own set of errors and exceptions. One common error that Python developers often encounter is the "KeyError". In this article, we will explore what a K
5 min read