count files in a directory

R

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
 
J

James Stroud

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/
 
S

Scott David Daniels

James said:
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
(e-mail address removed)
 
R

rbt

James said:
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 ([email protected]) - 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
 
R

rbt

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

Here's another Tback:
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
 
J

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.listdir('.') if os.path.isfile(f)])


Here's another Tback:

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/
 
H

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 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.
HTH!

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

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

iD8DBQBCjvUvf0bpgh6uVAMRAuuAAJwOwIgb9Ir3pTZex7dqc65FiOpd9QCfTA+x
FHMoRzqJiL6cVP9n6NwmpkM=
=s6qV
-----END PGP SIGNATURE-----
 
R

rbt

Heiko said:
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.

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?
 
R

rbt

James said:
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.
 
S

Steven Bethard

rbt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top