Alternative to os.listdir()/os.stat()

Tim Roberts timr at probo.com
Thu Jan 23 23:15:04 EST 2003


"Alfredo P. Ricafort" <alpot at mylinuxsite.com> wrote:
>
>I'm trying to get the files under a directory and its details. What  I
>did is to call os.listdir() and then os.stat(). So my code looks
>something like this:
>
>
>    fileList=os.listdir(path)
>    for i in range(len(fileList)):
>        f=os.stat(path+'/'+j)

You can gain a non-trivial performance boost by replacing that last line
with:
	f = os.stat( "%s/%s" % (path.j) )
or:
	f = os.stat( os.path.join(path,j) )

Using the "+" operator on strings is surprisingly expensive.

>However, I noticed that this approach takes a long time when the
>directory contains lots of files. So what I am looking for now is a
>function that can give me the details is one go, much like 'ls -ltra' in
>UNIX. 

You realize, of course that "ls -ltra" does exactly what your loop does?
The only difference is that it's in C.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.




More information about the Python-list mailing list