how to count lines in a file ?

Delaney, Timothy tdelaney at avaya.com
Wed Jul 24 20:00:05 EDT 2002


Damn - what happened there? Somehow I got a paragraph in the wrong place,
and an extra .sig ...

> From: Delaney, Timothy [mailto:tdelaney at avaya.com]
>
> > From: Richard Jones [mailto:rjones at ekit-inc.com]
> >
> > On Thu, 25 Jul 2002 8:31 am, Delaney, Timothy wrote:
> > > > From: Bo M. Maryniuck [mailto:b.maryniuk at forbis.lt]
> > > >
> > > > print len(open('/etc/passwd').readlines())
> >
> > Sorry, I'm coming in to this thread halfway through, but I
> > need to know why
> > that code is broken? Is it the
> > "open('/etc/passwd').readlines()"? That's a
> > very common pattern, as you note, because people think it is
> > perfectly
> > legitemate. What about it is broken? Why isn't it pointed out
> > more often that
> > it _is_ broken?
>
> We had this discussion just recently, but yes, that is
> precisely what is
> broken.
>
> What is broken? The file is never explicitly closed.
>
> Why is it broken? There are no guarantees that the file will be closed
> either:
>
> 1. Immediately (the assumption in code that uses the above construct);
>
> 2. In a timely manner;
>
> 3. Before the interpreter exits;
>
> 4. Before the computer is turned off.
>
> You can usually gather a guarantee of 4 from the OS (all
> files opened by a
> process are closed when that process exits) but you then have another
> problem:
>
>       open(filename).write(string)
>
> Now you not only have no guarantees that the file is closed,
> but also no
> guarantees that is the file is closed, it is flushed before
> closing. The
> most likely case where this occurs is at program exit - all files are
> closed, but not flushed.
>
> To date, many people have relied in (non-) guarantee #1 -
> that the file will
> be closed (and flushed) immediately.
>
> This in currently not true in Jython, and would not have been
> true with the
> proposed patch. Luckily for such people, there is a strong
> desire not to
> break "working" code, even if such code really is broken anyway. What
> happens if I try to use a module containing such code under Jython?
>
> It is explicitly documented that yo may not rely on reference-counting
> semantics for such things (Paul Rubin - take note please).
> 
> Tim Delaney
>
> --
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list