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:
- How to Solve Python TypeError: ‘tuple’ object is not callable.
- How to Solve Python TypeError: ‘bool’ object is not callable.
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.