os.listdir

eduardo.aguiar.developer eduardo.aguiar.developer at bol.com.br
Tue Sep 9 13:44:07 EDT 2003


> "Michael Peuser" <mpeuser at web.de> wrote in message news:<bjk10v$ue0$04$1 at news.t-online.com>...
> > "Graham Fawcett" <fawcett at teksavvy.com>
> > > Hari wrote:
> >  
> > > >What I wanted to know was, is it guaranteed that between 2 calls
> > > >os.listdir any files added to the directory are appended and the
> > > >earlier order is maintained?
> > 
> > As others pinted out, this is jighly improbably, espacially under windows
> > 
> > 
> > > There's no need for such sequence guarantees, though. It's probably not
> > > the most efficient but here's a way to do it:
> > >
> > >     import os
> > >     import time
> > >
> > >     somedir = '/tmp'
> > >     snapshot1 = os.listdir(somedir)
> > >     time.sleep(...)
> > >     snapshot2 = os.listdir(somedir)
> > >
> > >     newfiles = [f for f in snapshot2 if not f in snapshot1]
> > >
> > > Pretty efficient, really: that's an O(n) comparison if I remember my
> > > Python internals correctly. Which I don't, so don't trust my word for
> > > it. ;-)
> > 
> > 
> > You are wright to mistrust it ;-)
> > Though 'f in list' looks harmless it itself is o(n) because there seems to
> > be a linear search (This is different whith 'f in dict' of course). So your
> > list comprehension is of o(n*n).
> > 
> > Kindly
> > Michael P
> 
> D'oh! Thanks, Michael, I *knew* I'd messed that up... 
> 
> I'll repeat "list searches linear, dict lookups constant..." a hundred
> times before bed tonight.

I think you got something to repeat
the night after also :-)

"list searches linear, dict lookups
log2(n)"

 
__________________________________________________________________________
Acabe com aquelas janelinhas que pulam na sua tela.
AntiPop-up UOL - É grátis!
http://antipopup.uol.com.br/







More information about the Python-list mailing list