Sorting Directories from files in a os.listdir??

David Harrison dave.l.harrison at gmail.com
Thu Apr 10 06:12:02 EDT 2008


On 10/04/2008, Soren <soren.skou.nielsen at gmail.com> wrote:
> Hi,
>
>  I'd like to read the filenames in a directory, but not the
>  subdirectories, os.listdir() gives me everything... how do I separate
>  the directory names from the filenames? Is there another way of doing
>  this?

The only thing I can think of if you just want the immediate dir is to
use the os.path module's function isfile to test each item from the
list returned by os.listdir.

This should do the trick I think:

[ f for f in os.listdir('pathname') if os.path.isfile(f) ]

cheers
Dave



More information about the Python-list mailing list