student question

Discussion in 'Python' started by John, Jan 8, 2011.

  1. John

    John Guest

    >>> q_file = open(questions_location) #opens the document successfully
    >>> for line in q_file:

    print line

    # prints document successfully
    >>> line

    # prints last line of document
    >>> for line in q_file:

    print line # prints nothing

    ....why does it print nothing?
     
    John, Jan 8, 2011
    #1
    1. Advertising

  2. On 01/07/2011 09:42 PM, John wrote:
    >>>> q_file = open(questions_location) #opens the document successfully
    >>>> for line in q_file:

    > print line
    >
    > # prints document successfully
    >>>> line

    > # prints last line of document
    >>>> for line in q_file:

    > print line # prints nothing
    >
    > ...why does it print nothing?


    IIRC, iterating through the lines in a file moves the cursor (is that
    the correct term?) to the end of the file. After the first one, use
    q_file.seek(0) to go back to the start. I think.

    ~Corey Richardson
     
    Corey Richardson, Jan 8, 2011
    #2
    1. Advertising

  3. John

    John Guest

    On Jan 7, 6:47 pm, Corey Richardson <> wrote:
    > On 01/07/2011 09:42 PM, John wrote:
    >
    > >>>> q_file = open(questions_location) #opens the document successfully
    > >>>> for line in q_file:

    > >            print line

    >
    > > # prints document successfully
    > >>>> line

    > > # prints last line of document
    > >>>> for line in q_file:

    > >            print line # prints nothing

    >
    > > ...why does it print nothing?

    >
    > IIRC, iterating through the lines in a file moves the cursor (is that
    > the correct term?) to the end of the file. After the first one, use
    > q_file.seek(0) to go back to the start. I think.
    >
    > ~Corey Richardson


    fantastic. thanks, corey!
     
    John, Jan 8, 2011
    #3
  4. On Fri, 7 Jan 2011 18:42:45 -0800 (PST), John <> wrote:
    >>>> q_file = open(questions_location) #opens the document successfully
    >>>> for line in q_file:

    > print line
    >
    > # prints document successfully
    >>>> line

    > # prints last line of document
    >>>> for line in q_file:

    > print line # prints nothing
    >
    > ...why does it print nothing?


    open(filename) returns an iterator, not a list. Once you
    have exhausted the iterator, it stays exhausted.


    --
    To email me, substitute nowhere->spamcop, invalid->net.
     
    Peter Pearson, Jan 8, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chen Bin

    A student's question

    Chen Bin, Aug 31, 2003, in forum: VHDL
    Replies:
    7
    Views:
    538
  2. GoodProggie101

    SystemC : Can a CS student do it?

    GoodProggie101, Feb 29, 2004, in forum: VHDL
    Replies:
    0
    Views:
    443
    GoodProggie101
    Feb 29, 2004
  3. Hagar
    Replies:
    8
    Views:
    399
    Default User
    Apr 21, 2004
  4. Sue
    Replies:
    8
    Views:
    151
    Thomas 'PointedEars' Lahn
    Dec 13, 2003
  5. Sue
    Replies:
    5
    Views:
    180
Loading...

Share This Page