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 10 22862
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/
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/
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
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
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
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/
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-----
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?
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gary Feldman |
last post by:
Since I prefer to keep all my installed programs in one place, I went ahead
and installed Python in Program Files/Python. The command line shell seems
to work just fine, and I'm able to import...
|
by: Bill |
last post by:
I have looked up and down for a solution to this problem without any
luck.
I sell stuff on ebay. ebay will allow me to place javascript in my
listings which I use to show a series of thumbnails of...
|
by: Sue |
last post by:
How can I count the number of files in a folder?
Thanks for all help!!
Sue
|
by: JuLiE Dxer |
last post by:
Is there a way to programmatically determine if MCE isn't saving the
recorded tv video files in the default directory?
It'd be nice to know if the saved files aren't in the
C:\Documents and...
|
by: Don |
last post by:
I intermittently get a runtime Compilation Error that says 'The
compiler failed with error code 2000'. It appears that a DLL cannot
be found in the 'temporary asp.net files' directory.
The...
|
by: glub glub |
last post by:
i'm trying to make a program that works as Replace works in MS Word but this
is for use with files, not a text document.
FolderBrowserDialog1.ShowDialog()
txtPath.Text =...
|
by: Shawn Mehaffie |
last post by:
I have an application I want to be able to:
1) Store user specifc settings in ther "My Documents".
2) Store some information in "All Users" document directory.
3) I also want to be able to...
|
by: laredotornado |
last post by:
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the...
|
by: umut.tabak |
last post by:
Dear all,
We have a multi-platform application(Windows-Linux). Linux part of our
application is writing some input and trigger files on the a shared
drive. What I want to do is to be able to...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |