I am trying to reuse an opened file which is declared inside the constructor instance of a class but I guess I am doing something logically wrong. For instance consider the following example
class Temp:
def __init__(self):
self.open_file_ = open('periodic_status','r')
def function1(self):
new_file = self.open_file_
for i in new_file:
print 'test1'
def function2(self):
for j in self.open_file_:
print 'test2'
if __name__ == '__main__':
obj1 = Temp()
obj1.function1()
obj1.function2()
In the above program I can print test1 but I am not able to print the statement test2. Can some one explain me the logic.
Thanks
Because your file handle has exhausted all the lines in the file. You need to rewind it in “function2” using:
to start over again
See here docs.python.org