How do you limit the # of lines Read?

Paul McNett p at ulmcnett.com
Tue Sep 18 23:11:23 CEST 2007


koutoo at hotmail.com wrote:
> I am working on a loop for my code and was wondering if there is a way
> to limit the number of lines read through?  I'd hate to cut the test
> file in order to run the code in the testing phase.  Can you add a
> value in the parenthesis of the readline() function?
> 
> t = string.readline()  # Limit this somehow?

Assuming s is a file-like object:

for idx, line in enumerate(s):
         if idx > 2:
                 break
         print idx, line


-- 
pkm ~ http://paulmcnett.com



More information about the Python-list mailing list