Sorting Directories from files in a os.listdir??

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 10 06:14:46 EDT 2008


En Thu, 10 Apr 2008 06:55:13 -0300, Soren <soren.skou.nielsen 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




More information about the Python-list mailing list