如何使用Python计算目录中的文件数量
如何只计算目录中的文件?这将目录本身算作一个文件:
len(glob.glob('*'))
How do I count only the files in a directory? This counts the directory itself as a file:
len(glob.glob('*'))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
os.listdir()
比使用glob.glob
效率稍高。要测试文件名是否是普通文件(而不是目录或其他实体),请使用 os.path.isfile() :os.listdir()
will be slightly more efficient than usingglob.glob
. To test if a filename is an ordinary file (and not a directory or other entity), useos.path.isfile()
:对于所有类型的文件,包括子目录 (Python 2):
仅文件(避免子目录):
For all kind of files, subdirectories included (Python 2):
Only files (avoiding subdirectories):
这就是 fnmatch 非常方便的地方:
更多详细信息:http://docs.python.org/ 2/library/fnmatch.html
This is where fnmatch comes very handy:
More details: http://docs.python.org/2/library/fnmatch.html
如果你想计算目录中的所有文件 - 包括子目录中的文件,最Pythonic的方法是:
我们使用 sum 比显式添加文件计数(计时待定)更快
If you want to count all files in the directory - including files in subdirectories, the most pythonic way is:
We use sum that is faster than explicitly adding the file counts (timings pending)
使用 pathlib 的答案,而不将整个列表加载到内存中:
An answer with pathlib and without loading the whole list to memory:
简短而简单
Short and simple
我很惊讶没有人提到 os.scandir:
I am surprised that nobody mentioned
os.scandir
:这使用 os.listdir 并适用于任何目录:
这可以使用生成器进行简化,并通过以下方式加快速度:
This uses
os.listdir
and works for any directory:this can be simplified with a generator and made a little bit faster with:
虽然我同意 @DanielStutzbach 提供的答案:
os.listdir()
会比使用glob.glob
稍微高效一些。但是,如果您确实想计算文件夹中特定文件的数量,则需要额外的精度,您需要使用 len(glob.glob()) 。例如,如果您要计算要使用的文件夹中的所有 pdf:
While I agree with the answer provided by @DanielStutzbach:
os.listdir()
will be slightly more efficient than usingglob.glob
.However, an extra precision, if you do want to count the number of specific files in folder, you want to use
len(glob.glob())
. For instance if you were to count all the pdfs in a folder you want to use:这是一个简单的解决方案,可以计算包含子文件夹的目录中的文件数量。它可能会派上用场:
您应该得到与此类似的输出(当然,占位符已更改):
This is an easy solution that counts the number of files in a directory containing sub-folders. It may come in handy:
You should get an output similar to this (with the placeholders changed, of course):
摘自这篇文章
Taked from this post
一行和递归:
one liner and recursive:
这是一个我发现很有用的简单的单行命令:
Here is a simple one-line command that I found useful:
卢克的代码重新格式化。
Luke's code reformat.
我使用
glob.iglob
作为类似于以下两个选项返回4的目录结构(如预期,ie不计算子文件夹本身)
len(list (glob.iglob("data/train/*/*.png", recursive=True)))
sum(1 for i in glob.iglob("data/train/*/*.png) “))
I used
glob.iglob
for a directory structure similar toBoth of the following options return 4 (as expected, i.e. does not count the subfolders themselves)
len(list(glob.iglob("data/train/*/*.png", recursive=True)))
sum(1 for i in glob.iglob("data/train/*/*.png"))
这很简单:
它只是计算目录中的文件数量,我使用列表理解技术来迭代特定目录,返回所有文件。 “len(returned list)”返回文件的数量。
It is simple:
it simply counts number of files in directory , i have used list comprehension technique to iterate through specific directory returning all files in return . "len(returned list)" returns number of files.
如果您将使用操作系统的标准 shell,则可以比使用纯 pythonic 方式更快地获得结果。
Windows 示例:
If you'll be using the standard shell of the operating system, you can get the result much faster rather than using pure pythonic way.
Example for Windows:
我找到了另一个答案,它可能是正确的接受答案。
I found another answer which may be correct as accepted answer.
我编写的一个简单实用函数使用 os.scandir() 而不是 os.listdir()。
主要好处是,消除了对 os.path.is_file() 的需求,并替换为 os.DirEntry 实例的
is_file()
还消除了对 os.path.join(DIR, file_name) 的需要,如其他答案所示。A simple utility function I wrote that makes use of
os.scandir()
instead ofos.listdir()
.The main benefit is that, the need for
os.path.is_file()
is eliminated and replaced withos.DirEntry
instance'sis_file()
which also removes the need foros.path.join(DIR, file_name)
as shown in other answers.更简单的一个:
Simpler one:
我这样做了,这返回了文件夹中的文件数量(Attack_Data)...这工作正常。
i did this and this returned the number of files in the folder(Attack_Data)...this works fine.
我发现有时我不知道是否会收到文件名或文件路径。所以我打印了 os walk 解决方案输出:
out:
注意你可能需要排序。
I find that sometimes I don't know if I will receive filenames or the path to the file. So I printed the os walk solution output:
out:
note you might have to sort.
我想扩展@Mr_and_Mrs_D 的回复:
这会计算文件夹及其子文件夹中的所有文件。但是,如果您想做一些过滤 - 例如只计算以
.svg
结尾的文件,您可以这样做:您基本上将:
len(files)
替换为:
len([f for f in files if f.endswith('.svg')])
I would like to extend the reply from @Mr_and_Mrs_D:
This counts all the files in the folder and its subfolders. However, if you want to do some filtering - like only counting the files ending in
.svg
, you can do:You basically replace:
len(files)
with:
len([f for f in files if f.endswith('.svg')])
将其转换为列表,之后您可以使用 len() 函数:
Convert it to a list, after that you can make use of the
len()
function: