添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

TypeError: '_io.TextIOWrapper' object is not subscriptable

avatar
Borislav Hadzhiev

Last updated: Feb 1, 2023
4 min

banner

# Table of Contents

  1. TypeError: ' _ io.TextIOWrapper' object is not subscriptable
  2. 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.

typeerror io textiowrapper object is not subscriptable

Here is an example of how the error occurs.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: # ⛔️ TypeError: '_io.TextIOWrapper' object is not subscriptable 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.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: # ✅ get list of all lines lines = f.readlines() print(lines) print(lines[0]) for line in lines: print(line)

use readlines method to solve the error

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.

example.json
{ "name": "Alice", "age": 30

If we try to access a key without having parsed the data we would get the error.

main.py
file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: # ⛔️ TypeError: '_io.TextIOWrapper' object is not subscriptable print(f['name'])

# Parse the data before accessing properties

To get around this, we have to use the json.load() method.

main.py
import json with open('example.json', 'r', encoding='utf-8') as f: # ✅ deserialize file to Python object my_data = json.load(f) print(my_data) # 👉️ {'name': 'Alice', 'age': 30} print(my_data['name']) # 👉️ 'Alice' print(my_data['age']) # 👉️ 30

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.

main.py
a_list = ['bobby', 'hadz', 'com'] # 👇️ <built-in method __getitem__ of list object at 0x7f71f3252640> 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.

typeerror io textiowrapper object is not callable

Here is an example of how the error occurs.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: # ⛔️ TypeError: '_io.TextIOWrapper' object is not callable 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.

main.py
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')

use write method to write to file

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 .

main.py
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.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: # [... 'close', 'closed', 'detach', 'encoding', 'errors', # 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', # 'name', 'newlines', 'read', 'readable', 'readline', # 'write', 'write_through', 'writelines' ...]