Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:13:05+00:00 2026-05-22T16:13:05+00:00

I am trying to reuse an opened file which is declared inside the constructor

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:13:06+00:00Added an answer on May 22, 2026 at 4:13 pm

    Because your file handle has exhausted all the lines in the file. You need to rewind it in “function2” using:

    f.seek(0)
    

    to start over again

    See here docs.python.org

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reuse code from another project, but while copying the classes
I am trying to reuse a framelayout that has an image and textview inside
I'm trying to reuse the HTTPService object in a flex app but I'm running
I am trying to reuse an existing code ... but with no success .
I'm trying to reuse a function i made, but with any help yet. Here
Well, I'm trying to reuse a portion of C# code. It's an abstract class
I'm trying to reuse some script that I have working on another page, but
I'm trying to add an effect to style in order to reuse it, but
I'm trying to reuse a library function we have in house, but due to
I'm trying to reuse the same code I've always used but now it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.