Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > student question

Reply
Thread Tools

student question

 
 
John
Guest
Posts: n/a
 
      01-08-2011
>>> 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?
 
Reply With Quote
 
 
 
 
Corey Richardson
Guest
Posts: n/a
 
      01-08-2011
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
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      01-08-2011
On Jan 7, 6:47*pm, Corey Richardson <kb1...@aim.com> 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!
 
Reply With Quote
 
Peter Pearson
Guest
Posts: n/a
 
      01-08-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Student question on external calls. Hagar C++ 8 04-20-2004 11:39 PM
SystemC : Can a CS student do it? GoodProggie101 VHDL 0 02-29-2004 06:15 PM
REQ: Question on how to call functions (New JavaScript Student) Sue Javascript 5 12-15-2003 05:24 AM
REQ: New Question Form ( new JavaScript Student ) Sue Javascript 8 12-13-2003 11:37 PM
A student's question Chen Bin VHDL 7 09-04-2003 04:39 PM



Advertisments