Suppose I have a text file called 'text.txt' which contains:
Apple
Orange
Strawberry
Total of 3 lines and I have a class:
class ReadTextFile:
def __init__(self, filename):
self.filename = (open(filename, 'r'))
def number_of_lines(self):
num = 0
for line in self.filename:
line = line.strip("\n")
num += 1
return(num)
When I use this class to count the total number of lines, on the first try it gives me a correct answer which is '3', but after that it gives me '0' unless I redefine the variable "text":
>>> text = ReadTextFile('text.txt')
>>> text.number_of_lines()
3
>>> text.number_of_lines()
0
>>> text.number_of_lines()
0
...
>>> text = ReadTextFile('text.txt')
>>> text.number_of_lines()
3
>>> text.number_of_lines()
0
...
What am I doing wrong here?