How to separate directory list and file list?

Fredrik Lundh fredrik at pythonware.com
Sun Oct 23 11:27:49 EDT 2005


"Gonnasi" wrote:

> With
> >glob.glob("*")
>
> or
> >os.listdir(cwd)
>
> I can get a combined file list with directory list, but I just wanna a
> bare file list, no directory list. How to get it?

use os.path.isfile on the result.

    for file in glob.glob("*"):
        if not os.path.isfile(file):
            continue
        ... deal with file ...

    for file in os.listdir(cwd):
        file = os.path.join(cwd, file)
        if not os.path.isfile(file):
            continue
        ... deal with file ...

    files = map(os.path.isfile, glob.glob("*"))

    files = (file for file in os.listdir(cwd) if os.path.isfile(os.path.join(cwd, file)))

etc.

</F>






More information about the Python-list mailing list