[Baypiggies] reading files quickly and efficiently

Ned Deily nad at acm.org
Thu Nov 18 04:43:39 CET 2010


In article 
<AANLkTinTGwaqmRt+1rJWvjY6ooqQPfUDtNV1QqaB_i05 at mail.gmail.com>,
 wesley chun <wescpy at gmail.com> wrote:
> MODIFIED:
> f = open ('nr', 'r')
> print sum(1 for line in f)
> f.close()
> 
> can anyone else improve on this?

with open('nr', 'r') as f:
  print(sum(1 for line in f))

should work on any Python from 2.6 to 3.2, and 2.5 with
  from __future__ import with_statement

-- 
 Ned Deily,
 nad at acm.org



More information about the Baypiggies mailing list