Reputation: 15
I run this code:
file=open(filename, 'r')
line=filename.readline()
totallines=0
total=0
while line != "":
amount=float(line)
print(format(amount, '.2f'))
line=filename.readline()
totallines+=1
total+=amount
avg=total/totallines
print("The average of the numbers is", format(avg, '.2f'))
and it gives me this error AttributeError: 'str' object has no attribute 'read'
I dont understand what Im doing wrong?
Upvotes: 1
Views: 5386
Reputation: 4664
You are calling filename which is a string instead of file in here
file=open(filename, 'r')
line=filename.readline()
Should be line=file.readline()
In order to improve the code I suggest you use with and as which is faster in execution as well besides being more readable:
with open(filename, 'r') as file
line=file.readline()
Upvotes: 3