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 7308395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:05+00:00 2026-05-28T23:33:05+00:00

#1 input_file = ‘my-textfile.txt’ current_file = open(input_file) print current_file.readline() print current_file.readline() #2 input_file =

  • 0
#1
input_file = 'my-textfile.txt'
current_file = open(input_file)
print current_file.readline()
print current_file.readline()

#2
input_file = 'my-textfile.txt'
print open(input_file).readline()
print open(input_file).readline()

Why is it that #1 works fine and displays the first and second line, but #2 prints 2 copies of the first line and doesn’t print the same as #1 ?

  • 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-28T23:33:06+00:00Added an answer on May 28, 2026 at 11:33 pm

    When you call open you are opening the file anew and starting from the first line. Every time you call readline on an already open file it moves its internal “pointer” to the start of the next line. However, if you re-open the file the “pointer” is also re-initialized – and when you call readline it reads the first line again.

    Imagine that open returned a file object that looked like this:

    class File(object):
        """Instances of this class are returned by `open` (pretend)"""
    
        def __init__(self, filesystem_handle):
            """Called when the file object is initialized by `open`"""
    
            print "Starting up a new file instance for {file} pointing at position 0.".format(...)
    
            self.position = 0
            self.handle = filesystem_handle
    
    
        def readline(self):
            """Read a line. Terribly naive. Do not use at home"
    
            i = self.position
            c = None
            line = ""
            while c != "\n":
                c = self.handle.read_a_byte()
                line += c
    
            print "Read line from {p} to {end} ({i} + {p})".format(...)
    
            self.position += i
            return line
    

    When you ran your first example you would get something like the following output:

    Starting up a new file instance for /my-textfile.txt pointing at position 0.
    Read line from 0 to 80 (80 + 0)
    Read line from 80 to 160 (80 + 80)
    

    While the output of your second example would look something like this:

    Starting up a new file instance for /my-textfile.txt pointing at position 0.
    Read line from 0 to 80 (80 + 0)
    Starting up a new file instance for /my-textfile.txt pointing at position 0.
    Read line from 0 to 80 (80 + 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input file that I want to sort based on timestamp which
When reading data from the Input file I noticed that the ¥ symbom was
I have an input file and the first line contains the following decimal. 0.5053102074297753
I am trying to open input file dialog after clicking on link. I made
So I have an input file. It consists of 40 numbers. The first 20
From an input file I'm suppose to extract only first name of the student
I have an open input file stream. It is able to open the other
This is my input file .. [root@localhost scripts]# cat ip6hdr.txt | xargs -n4 6000
I have an input file with 3 columns, and I wanted to print lines
I have some input file that looks like this: asdfasfasfas.... asdfasdfasd.... asdfasdf.... asdfsadf... I

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.