Re: Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-23 Thread Terry Reedy
On 6/22/2015 9:32 PM, Paul Rubin wrote: Travis Griggs writes: The following seems to obtuse/clever for its own good: return sum(1 for _ in self.path.iterdir()) I disagree. For one who understands counting and Python, this is a direct way to define the count of a finite iterable. A fun

Re: Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-23 Thread Peter Otten
Travis Griggs wrote: > Subject nearly says it all. > > If i’m using pathlib, what’s the simplest/idiomatic way to simply count > how many files are in a given directory? > > I was surprised (at first) when > > len(self.path.iterdir()) > > I don’t say anything on the in the .stat() object t

Re: Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-23 Thread Laura Creighton
I use len(list(self.path.iterdir())) You get an extra list created in there. Do you care? Laura -- https://mail.python.org/mailman/listinfo/python-list

Re: Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-22 Thread Paul Rubin
Travis Griggs writes: > The following seems to obtuse/clever for its own good: > return sum(1 for _ in self.path.iterdir()) I've generally done something like that. I suppose it could be added to itertools. -- https://mail.python.org/mailman/listinfo/python-list

Re: Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-22 Thread Ian Kelly
On Mon, Jun 22, 2015 at 4:33 PM, Travis Griggs wrote: > > > Subject nearly says it all. > > If i’m using pathlib, what’s the simplest/idiomatic way to simply count how > many files are in a given directory? > > I was surprised (at first) when > >len(self.path.iterdir()) > > didn’t work. len

Re: Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-22 Thread Travis Griggs
Subject nearly says it all. If i’m using pathlib, what’s the simplest/idiomatic way to simply count how many files are in a given directory? I was surprised (at first) when len(self.path.iterdir()) didn’t work. I don’t see anything in the .stat() object that helps me. I could of course

Simplest/Idiomatic way to count files in a directory (using pathlib)

2015-06-22 Thread Travis Griggs
Subject nearly says it all. If i’m using pathlib, what’s the simplest/idiomatic way to simply count how many files are in a given directory? I was surprised (at first) when len(self.path.iterdir()) I don’t say anything on the in the .stat() object that helps me. I could of course do the 4

Re: count files in a directory

2005-05-21 Thread Steven Bethard
rbt wrote: > Heiko Wundram wrote: > import os > path = "/home/heiko" > file_count = sum((len(f) for _, _, f in os.walk(path))) > file_count > > Thanks! that works great... is there any significance to the underscores > that you used? I've always used root, dirs, files when using o

Re: count files in a directory

2005-05-21 Thread rbt
James Stroud wrote: > Sorry, I've never used os.walk and didn't realize that it is a generator. > > This will work for your purposes (and seems pretty fast compared to the > alternative): > > file_count = len(os.walk(valid_path).next()[2]) Thanks James... this works *really* well for times when

Re: count files in a directory

2005-05-21 Thread rbt
Heiko Wundram wrote: > Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud: > >>This will work for your purposes (and seems pretty fast compared to the >>alternative): >> >>file_count = len(os.walk(valid_path).next()[2]) > > > But will only work when you're just scanning a single directory with

Re: count files in a directory

2005-05-20 Thread Heiko Wundram
Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud: > This will work for your purposes (and seems pretty fast compared to the > alternative): > > file_count = len(os.walk(valid_path).next()[2]) But will only work when you're just scanning a single directory with no subdirectories...! The altern

Re: count files in a directory

2005-05-20 Thread James Stroud
Sorry, I've never used os.walk and didn't realize that it is a generator. This will work for your purposes (and seems pretty fast compared to the alternative): file_count = len(os.walk(valid_path).next()[2]) The alternative is: import os import os.path file_count = len([f for f in os.listdi

Re: count files in a directory

2005-05-20 Thread James Stroud
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/ -- http://mail.python.org/mailman/listinfo/python-list

Re: count files in a directory

2005-05-20 Thread James Stroud
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 >

count files in a directory

2005-05-20 Thread rbt
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."