count files in a directory

James Stroud jstroud at mbi.ucla.edu
Sat May 21 04:37:57 CEST 2005


On Friday 20 May 2005 07:12 pm, rbt wrote:
> I assume that there's a better way than this to count the files in a
> directory recursively. Is there???
>
> def count_em(valid_path):
>      x = 0
>      for root, dirs, files in os.walk(valid_path):
>          for f in files:
>              x = x+1
>      print "There are", x, "files in this directory."
>      return x
>
> rbt

def count_em(valid_path):
  root, dirs, files = os.walk(valid_path)
  return len(files)



-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list