469,936 Members | 2,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

count files in a directory

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."
return x

rbt
Jul 19 '05 #1
10 21752
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/
Jul 19 '05 #2

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/
Jul 19 '05 #3
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
Sc***********@Acm.Org
Jul 19 '05 #4
rbt
James Stroud wrote:
Come to think of it

file_count = len(os.walk(valid_path)[2])


I get this traceback:

PythonWin 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2004 Mark Hammond (mh******@skippinet.com.au) - see
'Help/About PythonWin' for further copyright information.
Traceback (most recent call last):
File "C:\Program
Files\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 310,
in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\rbt\Desktop\newa\replicate.py", line 57, in ?
A = count_em(X)
File "C:\Documents and Settings\rbt\Desktop\newa\replicate.py", line 51, in count_em
count = len(os.walk(valid_path)[2])
TypeError: unsubscriptable object
Jul 19 '05 #5
rbt
James Stroud wrote:
def count_em(valid_path):
root, dirs, files = os.walk(valid_path)
return len(files)


Here's another Tback:
Traceback (most recent call last):

File "C:\Program
Files\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 310,
in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\rbt\Desktop\newa\replicate.py", line 62, in ?
A = count_em(X)
File "C:\Documents and Settings\rbt\Desktop\newa\replicate.py", line 56, in count_em
root, dirs, files = os.walk(valid_path)
ValueError: need more than 2 values to unpack

Jul 19 '05 #6
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.listdir('.') if os.path.isfile(f)])
On Friday 20 May 2005 08:08 pm, rbt wrote:
James Stroud wrote:
def count_em(valid_path):
root, dirs, files = os.walk(valid_path)
return len(files)


Here's another Tback:
>>> Traceback (most recent call last):


File "C:\Program
Files\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" ,
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\rbt\Desktop\newa\replicate.py", line 62,
in ? A = count_em(X)
File "C:\Documents and Settings\rbt\Desktop\newa\replicate.py", line 56,
in count_em root, dirs, files = os.walk(valid_path)
ValueError: need more than 2 values to unpack


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

http://www.jamesstroud.com/
Jul 19 '05 #7
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 alternative (which will work regardless of subdirectories) is something
like the following:

heiko@heiko ~ $ python
Python 2.4 (#1, Apr 3 2005, 00:49:51)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r1, ssp-3.4.3.20050110-0,
pie- on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
path = "/home/heiko"
file_count = sum((len(f) for _, _, f in os.walk(path)))
file_count 55579


HTH!

--
--- Heiko.
see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCjvUvf0bpgh6uVAMRAuuAAJwOwIgb9Ir3pTZex7dqc6 5FiOpd9QCfTA+x
FHMoRzqJiL6cVP9n6NwmpkM=
=s6qV
-----END PGP SIGNATURE-----

Jul 19 '05 #8
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 no
subdirectories...!

The alternative (which will work regardless of subdirectories) is something
like the following:

heiko@heiko ~ $ python
Python 2.4 (#1, Apr 3 2005, 00:49:51)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r1, ssp-3.4.3.20050110-0,
pie- on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
path = "/home/heiko"
file_count = sum((len(f) for _, _, f in os.walk(path)))
file_count


55579
HTH!


Thanks! that works great... is there any significance to the underscores that you
used? I've always used root, dirs, files when using os.walk() do the underscores make
it faster... or more efficient?
Jul 19 '05 #9
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 I only need to count files in
the current directory (no recursion). I think others will find it useful as well.
Jul 19 '05 #10
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 os.walk()
do the underscores make it faster... or more efficient?


No, they don't make any semantic difference; they work just like any
other identifier. It's just that, by convention, single underscores
indicate that we don't care what values these names are bound to. So in
the example above, Heiko indicates that we don't care about what you
would normally call 'root' and 'dirs'.

HTH,

STeVe
Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sue | last post: by
3 posts views Thread by glub glub | last post: by
1 post views Thread by Shawn Mehaffie | last post: by
4 posts views Thread by laredotornado | last post: by
3 posts views Thread by umut.tabak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.