Python 计算目录和子目录中的文件夹数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29769181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count the number of folders in a directory and subdirectories
提问by Luke Willmer
I've got a script that will accurately tell me how many files are in a directory, and the subdirectories within. However, I'm also looking into identify how many folders there are within the same directory and its subdirectories...
我有一个脚本可以准确地告诉我一个目录中有多少文件,以及其中的子目录。但是,我也在研究确定同一目录及其子目录中有多少文件夹......
My current script:
我目前的脚本:
import os, getpass
from os.path import join, getsize
user = 'Copy of ' + getpass.getuser()
path = "C://Documents and Settings//" + user + "./"
folder_counter = sum([len(folder) for r, d, folder in os.walk(path)])
file_counter = sum([len(files) for r, d, files in os.walk(path)])
print ' [*] ' + str(file_counter) + ' Files were found and ' + str(folder_counter) + ' folders'
This code gives me the print out of: [*] 147 Files were found and 147 folders
.
这段代码给了我打印出来的:[*] 147 Files were found and 147 folders
。
Meaning that the folder_counter
isn't counting the right elements. How can I correct this so the folder_counter
is correct?
这意味着folder_counter
没有计算正确的元素。我怎样才能纠正这一点,所以这folder_counter
是正确的?
采纳答案by jonrsharpe
I think you want something like:
我想你想要这样的东西:
import os
files = folders = 0
for _, dirnames, filenames in os.walk(path):
# ^ this idiom means "we won't be using this value"
files += len(filenames)
folders += len(dirnames)
print "{:,} files, {:,} folders".format(files, folders)
Note that this only iterates over os.walk
once, which will make it much quicker on paths containing lots of files and directories. Running it on my Python directory gives me:
请注意,这只迭代os.walk
一次,这将使包含大量文件和目录的路径更快。在我的 Python 目录上运行它给了我:
30,183 files, 2,074 folders
which exactly matches what the Windows folder properties view tells me.
这与 Windows 文件夹属性视图告诉我的完全匹配。
Note that your current code calculates the same number twice because the only changeis renaming one of the returned values from the call to os.walk
:
请注意,您当前的代码计算了两次相同的数字,因为唯一的变化是将调用返回的值之一重命名为os.walk
:
folder_counter = sum([len(folder) for r, d, folder in os.walk(path)])
# ^ here # ^ and here
file_counter = sum([len(files) for r, d, files in os.walk(path)])
# ^ vs. here # ^ and here
Despite that name change, you're counting the same value (i.e. in both it's the third of the three returned values that you're using)! Python functions do not knowwhat names (if any at all; you could do print list(os.walk(path))
, for example) the values they return will be assigned to, and their behaviour certainly won't change because of it. Per the documentation, os.walk
returns a three-tuple (dirpath, dirnames, filenames)
, and the names you use for that, e.g. whether:
尽管更改了名称,但您计算的是相同的值(即,它是您使用的三个返回值中的第三个)!Python 函数不知道print list(os.walk(path))
它们返回的值将被分配给什么名称(如果有的话;例如,您可以这样做),并且它们的行为肯定不会因此而改变。根据文档,os.walk
返回一个三元组(dirpath, dirnames, filenames)
,以及您为此使用的名称,例如:
for foo, bar, baz in os.walk(...):
or:
或者:
for all_three in os.walk(..):
won't change that.
不会改变这一点。
回答by Vighnesh Birodkar
回答by Xxxo
Python 2.7 solution
Python 2.7 解决方案
For a single directory and in you can also do:
对于单个目录,您还可以执行以下操作:
import os
print len(os.walk('dir_name').next()[1])
which will not load the whole string list and also return you the amount of directories inside the 'dir_name'
directory.
它不会加载整个字符串列表,并且还会返回目录中的'dir_name'
目录数量。
Python 3.x solution
Python 3.x 解决方案
Since many people just want an easy and fast solution, without actually understanding the solution, I edit my answer to include the exact working code for Python 3.x.
由于许多人只想要一个简单快速的解决方案,而没有真正理解该解决方案,因此我编辑了我的答案以包含 Python 3.x 的确切工作代码。
So, in Python 3.x we have the next
method instead of .next
. Thus, the above snippet becomes:
所以,在 Python 3.x 中,我们有next
方法而不是.next
. 因此,上面的代码片段变为:
import os
print(len(next(os.walk('dir_name'))[1]))
where dir_name
is the directory that you want to find out how many directories has inside.
dir_name
你想找出里面有多少目录的目录在哪里。