473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_p ath):
for f in files:
x = x+1
print "There are", x, "files in this directory."
return x

rbt
Jul 19 '05
10 22911
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2649
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 things like urllib and its dependencies. However, I can't get Idle to startup. When I try running it manually (i.e. python ...idle.py), I get E:Program: can't open file 'Files\Python\python.exe Obviously this is an error in in parsing the path...
2
1972
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 my item. In my script I have a variable that defines the number of pictures of that item. What I would like to do is have the script determine the number of pictures in the directory. I host the pictures on my own server and I can turn on...
1
13433
by: Sue | last post by:
How can I count the number of files in a folder? Thanks for all help!! Sue
0
1143
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 Settings\All Users\Documents\Recorded TV\ directory.
0
2095
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 Detailed Compiler Output is at the bottom of this post. This is a custom dll named UtilitiesINGR.dll that I put in the GAC. I added a machine.config entry to instruct my app to use the GAC entry. Now I need to remove the dll from the GAC so I can...
3
6409
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 = FolderBrowserDialog1.SelectedPath above are the 2 lines of code i have so far, i would like to display/add the files to a list view and i feel that i would have to count the amount of files in the specified directory to do so, so how would i count the amount of files...
1
1486
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 access program files that are installed in the "Program Files" directory. How can I get these locations from within a VB application.
4
30284
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 sub-directories. Thanks for any functions or one-liners. - Dave
3
3423
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 count the occurence of these trigger files. lets say my file is file.start
0
8162
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8605
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8246
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8415
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1413
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.