How to Solve Python TypeError: ‘_io.TextIOWrapper’ object is not callable

by | Programming, Python, Tips

This error occurs if you try to call a File object as if it were a function. If you put parenthesis immediately after the file object name, Python will interpret this as a function call. You can solve this error by calling a method belonging to the File object, for example, write().

with open('file.txt', 'w') as f:
    f.write('1st line \n')

This tutorial will go through how to solve this error with code examples.


TypeError: ‘_io.TextIOWrapper’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

File objects do not respond to a function call because they are not functions. If you try to call a file object, the Python interpreter will raise the TypeError: ‘_io.TextIOWrapper’ object is not callable.

Example

Let’s look at an example where we want to write a list of strings to a file using a context manager.

particles = ['muon', 'electron', 'tau']

with open('leptons.txt', 'w') as f_to_write:
    for particle in particles:
        f_to_write(particle)
        f_to_write('\n')

The variable particles contains the names of three particles. Within the context manager, we will attempt to iterate over the particle names and write each of them to the file called leptons.txt. Let’s run the code to see what happens:

------------------------------------------------------------------------
TypeError                              Traceback (most recent call last)
Input In [18], in <cell line: 3>()
      3 with open('leptons.txt', 'w') as f_to_write:
      4     for particle in particles:
----> 5         f_to_write(particle)
      6         f_to_write('\n')

TypeError: '_io.TextIOWrapper' object is not callable

The error occurs because we put parenthesis immediately after the variable f_to_write. Python interprets this syntax as a function call with particle as the argument, but f_to_write is a File object, not a function.

Solution

We can solve the error by calling the write() method on the f_to_write object and passing particle as the argument. Let’s look at the updated code:

particles = ['muon', 'electron', 'tau']

with open('leptons.txt', 'w') as f_to_write:
    for particle in particles:
        f_to_write.write(particle)
        f_to_write.write('\n')

Once we run the code we can see that the leptons.txt file contains the following text:

muon
electron
tau

Summary

Congratulations on reading to the end of this tutorial.

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!