. Advertisement .
..3..
. Advertisement .
..4..
I get an error
>>> f = open('goodlines.txt')
>>> mylist = f.splitlines()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'splitlines'
>>> mylist = f.split()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'split'
when trying to use the split() procedure for my project. How to fix the attributeerror: ‘_io.textiowrapper’ object has no attribute ‘split’ error? Please give me some advice.
The cause: The problem that cause the attributeerror: ‘_io.textiowrapper’ object has no attribute ‘split’ is that you’re not reading the file’s data. An open file object is being used with
str
methods.Solution:
Simply using
list()
on the file object allows you to read a file in a list of lines form:The newline characters are included here. These can be removed from a list comprehension:
This is what you can do:
open()
returns a file object. There is nosplitlines()
orsplit()
method for file object. To see all methods of file object, you could usedir(f)