os.listdir
Graham Fawcett
graham__fawcett at hotmail.com
Tue Sep 9 12:58:34 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.
-- Graham
More information about the Python-list
mailing list