'AttributeError: 'list' object has no attribute 'readlines' (Python 3.4.4)
I am currently trying to read a text file and then print it into the Python Shell. It reads the file perfectly fine, but when it goes the read the lines seperately it tells me there is a AttributeError. How could I fix this?
Here's my code:
import time
import linecache
print("Welcome to the League Fixture Manager!")
time.sleep(3)
print("What would you like to do?")
time.sleep(1)
print("Press A to search for a fixture.")
time.sleep(0.1)
print("Press B to view Outstanding fixtures.")
time.sleep(0.1)
print("Press C to display the leader board")
time.sleep(0.1)
print("Or press Q to quit, this will exit the application.")
time.sleep(0.1)
menuOptions = input("What would you like to do? A, B, C, or Q.")
if menuOptions == 'A':
print("Searching for fixtures...")
time.sleep(3)
data = [line.strip() for line in open("Y:/Computing & Business/Students/Computing/Year 10/CA 2017 Edexcel/firesideFixtures.txt").readlines()]
lines = data.readlines()
print(data)
time.sleep(1)
print("Return to menu?")
menuReturn = input("Y or N")
if menuReturn == 'Y':
print("Press B to view outstanding fixtures.")
time.sleep(0.1)
print("Press C to display the leaderboard")
time.sleep(0.1)
print("Or press Q to exit the application.")
time.sleep(0.1)
print("You cannot review the fixture list now you have seen it however you can scroll up to view it again.")
time.sleep(0.1)
menuOptions2 = input("What would you like to do? B, C, or Q?")
if menuOptions2 == 'B':
print("~~incomplete~~")
elif menuOptions2 == 'C':
print("~~incomplete~~")
elif menuOptions2 == 'Q':
print("Exiting Application...")
time.sleep(1)
exit()
elif menuReturn == 'N':
print("Exiting Application...")
time.sleep(2)
exit()
elif menuOptions == 'B':
print("~~incomplete~~")
elif menuOptions == 'C':
print("~~incomplete~~")
elif menuOptions == 'Q':
print("Exiting Applicaion...")
time.sleep(2)
exit()
And this is what I recieve:
Welcome to the League Fixture Manager!
What would you like to do?
Press A to search for a fixture.
Press B to view Outstanding fixtures.
Press C to display the leader board
Or press Q to quit, this will exit the application.
What would you like to do? A, B, C, or Q.A
Searching for fixtures...
Traceback (most recent call last):
File "E:\Python\Python Work\League\League3.py", line 20, in <module>
lines = data.readlines()
AttributeError: 'list' object has no attribute 'readlines'
Solution 1:[1]
You're trying to apply readlines
to a list. In your example, data
is already a list which contains the file lines. The line:
lines = data.readlines()
is redundant. You can remove it. More, bear with the fact that the method readlines()
reads until EOF using readline()
and already returns a list containing the lines.
More, I recommend using with()
when you work with files:
with open(some_file) as f:
....
The advantage of using a with
statement is that it is guaranteed to close the file (thing that you didn't do) no matter how the nested block exits. If an exception occurs before the end of the block, it will close the file before the exception is caught by an outer exception handler.
As per your printing request, just loop over the list and print it out:
for line in data:
print(line)
I recommend you read the official Python documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |