Determining whether it's a file or a directory?

Scott David Daniels Scott.Daniels at Acm.Org
Fri Oct 8 13:04:52 CEST 2004


Dfenestr8 wrote:
> os.listdir("path") returns the names of all the items in a directory ok,
> but I need to know whether the item is a file or a directory.
> 
> Can anybody tell me how this could be done?
Some others have shown you how to test if a file is a directory, as
well as how to get a separated list of files and directories, but
I'd like to point out a mistake you are making: you believe a name
is associated with either a file or a directory, but not both.  There
are already a few file systems where this is not true, and the trend
seems to be towards, rather than away from this.

The os.walk technique would automatically cope with this situation
(the same name could show up in both lists).  Using the os.path.isdir
technique, you are more likely to decide that "if something is a
directory, it is not a file (ie has no non-directory data)."

-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list