count files in a directory

Scott David Daniels Scott.Daniels at Acm.Org
Fri May 20 22:58:24 EDT 2005


James Stroud wrote:
> Come to think of it
> 
> file_count = len(os.walk(valid_path)[2])
> 
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
> 
> http://www.jamesstroud.com/

Somee possible answers are:

     # files directly in path
     file_count = len(os.walk(path)[2])

     # files and dirs directly in path
     file_count = len(os.listdir(path))

     # files in or below path
     file_count = 0
     for root, dirs, files in os.walk(path):
         file_count += len(files)


     # files and dirs in or below path
     file_count = 0
     for root, dirs, files in os.walk(path):
         file_count += len(files) + len(dirs)


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list