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

by | Programming, Python, Tips

This error occurs when you try to call the split() method on a File object. The split() method is an attribute of the String class, not _io.TextIOWrapper. You can solve this error by iterating over the File object using a for loop, for example:

for line in file:
    line.split()

Each line in the file is a string. Alternatively, you can call the read() method on the File object, which returns a string on which you can call the split() method.

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


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

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The split method is an attribute of the String class, not the _io.TextIOWrapper class.

Example

Consider the following text file containing the names of five celebrities:

Leonardo DiCaprio
Michael Jordan
Franz Kafka
Mahatma Gandhi
Albert Einstein

We will save the celebrity names under the filename ‘celeb_names.txt‘. Next, we will attempt to read the data into a program, split each name into first name and surname, and print the surnames. Let’s look at the code:

with open('celeb_names.txt', 'r') as f:
    first_names, surnames = f.split()
    print(surnames)
    

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
      1 with open('celeb_names.txt', 'r') as f:
----> 2     first_names, surnames = f.split()
      3     print(surnames)

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

The error occurs because we try to call split() on the File object returned by the open() function call. The split() method is an attribute of the String class. We can find out what attributes an object has with the built-in dir() function, for example:

with open('celeb_names.txt', 'r') as f:
    attributes = dir(f)
    print('split' in attributes)
False

We checked for membership of the split method in the list of attributes for the File object returned by the dir() function. The result is False, confirming that split is not a TextIOWrapper method.

Solution

We can solve this error by iterating over the lines in the File object using a for loop. Let’s look at the revised code:

with open('celeb_names.txt', 'r') as f:
    for line in f:
        print(type(line))
        first_name, surname = line.split()
        print(f'Celebrity surname: {surname}')

The f variable is a buffered text stream, and the expression for line in f iterates over the stream using the new line delimiter until it reaches the end-of-file. Each line is of type String, and we can call the split() method as it is a String method. We can verify the type of an object using the built-in type() method. Let’s run the code to see the result:

<class 'str'>
Celebrity surname: DiCaprio
<class 'str'>
Celebrity surname: Jordan
<class 'str'>
Celebrity surname: Kafka
<class 'str'>
Celebrity surname: Gandhi
<class 'str'>
Celebrity surname: Einstein

We successfully retrieved the surnames of each celebrity using the split() method.

Summary

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!

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!