How to Solve Python AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘replace’

by | Programming, Python, Tips

This error occurs when you try to call the string method replace() on a File object. You can solve this error by reading the file to get a string and calling the replace() method on the file content.

For example:

with open('artists.txt', 'r') as f:
    content = f.readlines()
    for line in content:
        line = line.replace('_',' ')
        print(line)

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


AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘replace’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. replace() is a string method which replaces a specified phrase with another specified phrase. All occurrences of the specified phrase are replaced by default. To replace a phrase a certain number of times, the count argument must be specified.

Example

Let’s look at an example to reproduce the error. First, consider the following text file called artists.txt containing the names of famous jazz musicians.

Miles_Davis
Gil_Evans
John_Coltrane
Charles_Mingus
Dizzy_Gillepsie

Next, we will attempt to read the data and replace the underscore between the first and last names with a space.

artists = open('artists.txt')
artists.replace('_','')

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 artists.replace('_','')

AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

The error occurs because replace() is a string method, not an attribute of _io.TextIOWrapper. We can check if a method exists for an object using the dir() method.

print(type(artists))
print('replace' in dir(artists))
_io.TextIOWrapper
False

The membership operation returns False, therefore replace() is not an attribute of _io.TextIOWrapper.

Solution

We can solve the error by using the context manager to open the file and then call the File object readlines() method to get the content as a list of strings.

We can then iterate over the list of strings and call the replace() method on each string.

with open('artists.txt', 'r') as f:

    content = f.readlines()

    for line in content:

        line = line.replace('_',' ')

        print(line)

Let’s run the code to see the result:

Miles Davis

Gil Evans

John Coltrane

Charles Mingus

Dizzy Gillepsie

Congratulations on reading to the end of this tutorial.

For further reading on errors involving TextIOWrapper, go to the article:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

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.

Buy Me a Coffee ✨