Sorting Directories from files in a os.listdir??

Soren soren.skou.nielsen at gmail.com
Thu Apr 10 06:30:53 EDT 2008


On Apr 10, 12:14 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Thu, 10 Apr 2008 06:55:13 -0300, Soren <soren.skou.niel... at gmail.com>
> escribió:
>
> > 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?
>
> Check each returned name using os.path.isfile
> (untested):
>
> def files_only(path):
>    return [filename for filename in os.listdir(path)
>            if os.path.isfile(os.path.join(path, filename))]
>
> --
> Gabriel Genellina

Thanks everyone! That worked! :)



More information about the Python-list mailing list