#
Table of Contents
-
TypeError: '
_
io.TextIOWrapper' object is not subscriptable
-
TypeError: '
_
io.TextIOWrapper' object is not callable
#
TypeError: '
_
io.TextIOWrapper' object is not subscriptable
The Python "TypeError: '
_
io.TextIOWrapper' object is not subscriptable"
occurs when we try to use square brackets to access a key or index in a file
object.
To solve the error, use the
readlines()
method if you need a list of the
file's lines, or parse the JSON before accessing a key.
Here is an example of how the error occurs.
with open('example.txt', 'r', encoding='utf-8') as f:
print(f[0])
We use square brackets to try to access the element at index
0
, but the
TextIOWrapper
object is not subscriptable.
#
Use the readlines() method to solve the error
In this scenario, we have to use the
readlines()
method to read all the lines
of the file.
with open('example.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
print(lines)
print(lines[0])
for line in lines:
print(line)
If you need to read all the lines of a file in a list, use the
f.readlines()
method.
Another common cause of the error is trying to read from a JSON file without parsing the content into a native Python object first.
Imagine we have an
example.json
file with the following content.
{
"name": "Alice",
"age": 30
If we try to access a key without having parsed the data we would get the error.
file_name = 'example.json'
with open(file_name, 'r', encoding='utf-8') as f:
print(f['name'])
#
Parse the data before accessing properties
To get around this, we have to use the
json.load()
method.
import json
with open('example.json', 'r', encoding='utf-8') as f:
my_data = json.load(f)
print(my_data)
print(my_data['name'])
print(my_data['age'])
The
json.load
method is
used to deserialize a file to a Python object.
The
json.load()
method expects a text file or a binary file containing a JSON document that implements a
.read()
method.
If you try to access a key without having parsed the file object into a native
Python object, the error occurs.
The error means that we are using square brackets to access a key in a specific
object or to access a specific index, however, the object doesn't support this
functionality.
You should only use square brackets to access subscriptable objects.
The subscriptable objects in Python are:
-
list
-
tuple
-
dictionary
-
string
All other objects have to be converted to a subscriptable object by using the
list()
,
tuple()
,
dict()
or
str()
classes to be able to use bracket
notation.
Subscriptable objects implement the
__getitem__
method whereas
non-subscriptable objects do not.
a_list = ['bobby', 'hadz', 'com']
print(a_list.__getitem__)
#
TypeError: '
_
io.TextIOWrapper' object is not callable
The Python: "TypeError: '
_
io.TextIOWrapper' object is not callable" occurs
when we try to call a file object as if it were a function.
To solve the error, make sure you don't have any clashes between function and
variable names and access properties on the file object instead.
Here is an example of how the error occurs.
with open('example.txt', 'r', encoding='utf-8') as f:
f()
We tried to call the
_io.TextIOWrapper
object as a function and got the error.
#
Use the
write()
method to write to a file
If you are trying to write to a file, use the
write()
method on the file
object.
file_name = 'example.txt'
with open(file_name, 'w', encoding='utf-8') as my_file:
my_file.write('first line' + '\n')
my_file.write('second line' + '\n')
my_file.write('third line' + '\n')
The code sample writes 3 lines to a file named
example.txt
.
#
Use a
for
loop to read from a file
If you need to read from a file, use a simple
for loop
.
with open('example.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
print(lines)
print(lines[0])
for line in lines:
print(line)
The code sample assumes that you have an
example.txt
file in the same
directory as your Python script and reads from the file.
#
Make sure you don't have clashes between function and variable names
Make sure you don't have any clashes with variables and functions sharing the
same name.
A good way to start debugging is to
print(dir(your_object))
and see what
attributes a
_io.TextIOWrapper
has.
Here is an example of what printing the attributes of a
_io.TextIOWrapper
looks like.
with open('example.txt', 'r', encoding='utf-8') as f: