how to know whether the item in a diretory is a file/directory

David Bolen db3l at
Wed Aug 1 05:30:38 CEST 2001

"sdf" <wqh-2 at> writes:

> I want to process all the files in a diretory,and ignore the
> subdirectorys in it ,I use os.listdir() to list all the items,but I
> do not know whether the selected item is a file

Simplest approach is probably to use os.path.isdir(path).  Note that
you may need to add the directory to the file returned from os.listdir
if you aren't listing files in the current directory.

There are several isxxx functions in os.path for various matches.
They're all basically wrappers for calling os.stat(filename), and then
checking the result[stat.ST_MODE] (the stat module has constants to
index into the tuple returned by os.stat) for certain bits (constants
and functions to test against which are also in the stat module).

-- David
 \               David Bolen            \   E-mail: db3l at  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \

More information about the Python-list mailing list