close

[Solved] Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’ Error Occurs?

Today I get the following error Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’ in python.

How To Solve Python: AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘split’ Error ?

  1. How To Solve Python: AttributeError: '_io.TextIOWrapper' object has no attribute 'split' Error ?

    To Solve Python: AttributeError: '_io.TextIOWrapper' object has no attribute 'split' Error You can read the file as a list of lines by simply calling list() on the file object:

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

    To Solve Python: AttributeError: '_io.TextIOWrapper' object has no attribute 'split' Error You can read the file as a list of lines by simply calling list() on the file object:

Solution 1

You are using str methods on an open file object.

You can read the file as a list of lines by simply calling list() on the file object:

with open('goodlines.txt') as f:
    mylist = list(f)

This does include the newline characters. You can strip those in a list comprehension:

with open('goodlines.txt') as f:
    mylist = [line.rstrip('\n') for line in f]

Solution 2

Try this:

 >>> f = open('goodlines.txt')
 >>> mylist = f.readlines()

open() function returns a file object. And for file object, there is no method like splitlines() or split(). You could use dir(f) to see all the methods of file object.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read