What causes and ways to resolve the TypeError: ‘_io.TextIOWrapper’ object is not subscriptable? If you are looking for a solution to this problem, this article is for you.
What causes the TypeError: ‘_io.TextIOWrapper’ object is not subscriptable?
The TypeError: ‘_io.TextIOWrapper’ object is not subscriptable occurs when you index an unserialized Json (.json) file object into a Python object or an unread text file (.txt) object.
Example:
[ {"id": 1, "name": "Apple"}, {"id": 2, "name": "Banana"}, {"id": 3, "name": "Coconut"}, {"id": 4, "name": "Water Melon"} ]
filePath = 'data.json' # Accessing the element index in the Json file without serializing the file will cause an error. with open(filePath, 'r') as fileObj: print(fileObj['0:'])
Output:
Traceback (most recent call last):
File "d:\vscode\Python\main.py", line 5, in <module>
print(fileObj['0:'])
~~~~~~~^^^^^^
TypeError: '_io.TextIOWrapper' object is not subscriptable
A similar error occurs for you to index a text file.
Example:
['John', 'Pete', 'Frank', 'Mary']
File main.py:
filePath = 'infor.txt' # Accessing the element index in the text file with open(filePath, 'r') as fileObj: print(fileObj[0:])
Output:
Traceback (most recent call last):
File "d:\vscode\Python\main.py", line 5, in <module>
print(fileObj[0:])
~~~~~~~^^^
TypeError: '_io.TextIOWrapper' object is not subscriptable
How to solve the TypeError: ‘_io.TextIOWrapper’ object is not subscriptable?
Remind a little bit about how to open the file:
Open the file with the ‘with’ statement in the above example. A little introduction about the ‘with’ statement for you to easily visualize the error. The ‘with’ command has the following structure:
with open(filepath, mode, encoding=None) as f:
processing command block
Parameters:
- open: is the
open()
function to open a file. - f: is the file object returned if the file is opened successfully.
The ‘with’ statement will automatically close the file, which is an advantage over opening the file with the open() function.
For a text file
Example:
- First, you need to read all the data in the info.txt file.
- Only then perform the index access.
filePath = 'infor.txt' with open(filePath, 'r') as fileObj: # Perform reading data from a text file using the readlines() method dataFile = fileObj.readlines() # Access the index of the data just read print(dataFile[0:])
Output:
['John', 'Pete', 'Frank', 'Mary']
For a Json file
Example:
- Serialize the Json file object using the
json.load()
function. - Performs index access only if the Json file object has been serialized to a Python object.
import json filePath = 'data.json' with open(filePath, 'r') as fileObj: # You need to serialize the Json file object to Python dataFile = json.load(fileObj) # Python object index access has just been serialized print(dataFile[0:])
Output:
[{'id': 1, 'name': 'Apple'}, {'id': 2, 'name': 'Banana'}, {'id': 3, 'name': 'Coconut'}, {'id': 4, 'name': 'Water Melon'}]
Use try/except
I’ll do it with the text file first. The data from the text file 'infor.txt'
is preserved.
Example:
filePath = 'infor.txt' try: with open(filePath, 'r') as fileObj: print(fileObj[0:]) except: with open(filePath, 'r') as fileObj: # Read the entire text file data using the readlines() function dataFile = fileObj.readlines() # Python object index access has just been serialized print(dataFile[0:])
Output:
['John', 'Pete', 'Frank', 'Mary']
Same for the Json file ‘data.json’.
Example:
import json filePath = 'data.json' try: with open(filePath, 'r') as fileObj: print(fileObj['0:']) except: with open(filePath, 'r') as fileObj: # You need to serialize the Json file object to Python dataFile = json.load(fileObj) # Python object index access has just been serialized print(dataFile[0:])
Output:
[{'id': 1, 'name': 'Apple'}, {'id': 2, 'name': 'Banana'}, {'id': 3, 'name': 'Coconut'}, {'id': 4, 'name': 'Water Melon'}]
Summary
That is the cause and handling of the TypeError: ‘_io.TextIOWrapper’ object is not subscriptable, I want to convey to you. The key point to solve this error is importing the datetime class from the datetime module. Hope you get it fixed soon.
Maybe you are interested:
- TypeError: object of type ‘int’ has no len() in Python
- TypeError: write() argument must be str, not bytes in Python
- TypeError: unsupported operand type(s) for /: list and int

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java