如何使用 Python 读取文件夹中的文件数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3883138/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:10:55  来源:igfitidea点击:

How do I read the number of files in a folder using Python?

pythonfile-io

提问by Noah R

How do I read the number of files in a specific folder using Python? Example code would be awesome!

如何使用 Python 读取特定文件夹中的文件数?示例代码会很棒!

采纳答案by Mark Byers

To count files and directories non-recursively you can use os.listdirand take its length.

要以非递归方式计算文件和目录,您可以使用os.listdir并取其长度。

To count files and directories recursively you can use os.walkto iterate over the files and subdirectories in the directory.

要递归计算文件和目录,您可以使用os.walk迭代目录中的文件和子目录。

If you only want to count files not directories you can use os.listdirand os.path.fileto check if each entry is a file:

如果您只想计算文件而不是目录,您可以使用os.listdiros.path.file检查每个条目是否是一个文件:

import os.path
path = '.'
num_files = len([f for f in os.listdir(path)
                if os.path.isfile(os.path.join(path, f))])

Or alternatively using a generator:

或者使用生成器:

num_files = sum(os.path.isfile(os.path.join(path, f)) for f in os.listdir(path))

Or you can use os.walkas follows:

或者您可以os.walk按如下方式使用:

len(os.walk(path).next()[2])

I found some of these ideas from this thread.

我从这个线程中找到了其中的一些想法。

回答by bstpierre

You can use the globmodule:

您可以使用glob模块:

>>> import glob
>>> print len(glob.glob('/tmp/*'))
10

Or, as Mark Byers suggests in his answer, if you only want files:

或者,正如 Mark Byers 在他的回答中所建议的,如果您只想要文件:

>>> print [f for f in glob.glob('/tmp/*') if os.path.isfile(f)]
['/tmp/foo']
>>> print sum(os.path.isfile(f) for f in glob.glob('/tmp/*'))
1

回答by slezica

Mark Byer's answer is simple, elegant, and goes along with the python spirit.

Mark Byer 的回答简单、优雅,并且符合蟒蛇精神。

There's a problem, however: if you try to run that for any other directory than ".", it will fail, since os.listdir() returns the names of the files, not the full path. Those two are the same when listing the current working directory, so the error goes undetected in the source above.

但是,有一个问题:如果您尝试为除“.”以外的任何其他目录运行它,它将失败,因为 os.listdir() 返回文件的名称,而不是完整路径。在列出当前工作目录时,这两个是相同的,因此在上面的源代码中没有检测到错误。

For example, if your at "/home/me" and you list "/tmp", you'll get (say) ['flashXVA67']. You'll be testing "/home/me/flashXVA67" instead of "/tmp/flashXVA67" with the method above.

例如,如果您在“/home/me”并列出“/tmp”,您将得到(例如)['flashXVA67']。您将使用上述方法测试“/home/me/flashXVA67”而不是“/tmp/flashXVA67”。

You can fix this using os.path.join(), like this:

您可以使用 os.path.join() 解决此问题,如下所示:

import os.path
path = './whatever'
count = len([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])

Also, if you're going to be doing this count a lot and require performance, you may want to do it without generating additional lists. Here's a less elegant, unpythonesque yet efficient solution:

此外,如果您要大量执行此计数并需要性能,则可能希望在不生成其他列表的情况下执行此操作。这是一个不那么优雅,非pythonesque但有效的解决方案:

import os

def fcount(path):
    """ Counts the number of files in a directory """
    count = 0
    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path, f)):
            count += 1

    return count


# The following line prints the number of files in the current directory:
path = "./whatever"
print fcount(path)

回答by Leonid Dashko

total = len(filter(
            lambda f: os.path.isfile(os.path.join(path_to_dir, f)),
            os.listdir(path_to_dir)))

OR

或者

total = sum([True for f in os.listdir(path_to_dir) if os.path.isfile(os.path.join([path_to_dir, f)])

回答by Bill Bell

pathlib, that is new in v. 3.4, makes like easier. The line labelled 1makes a non-recursive list of the current folder, the one labelled 2the recursive list.

pathlib,这是 v. 3.4 中的新功能,使操作更容易。标记为1的行生成当前文件夹的非递归列表,标记为2 的行是递归列表。

from pathlib import Path

import os
os.chdir('c:/utilities')

print (len(list(Path('.').glob('*')))) ## 1
print (len(list(Path('.').glob('**/*')))) ## 2

There are more goodies too. With these additional lines you can see both the absolute and relative file names for those items that arefiles.

还有更多的好东西。有了这些额外的线,你可以同时看到了那些物品的绝对和相对文件名文件。

for item in Path('.').glob('*'):
    if item.is_file():
        print (str(item), str(item.absolute()))

Result:

结果:

boxee.py c:\utilities\boxee.py
boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite
find RSS.py c:\utilities\find RSS.py
MyVideos34.sqlite c:\utilities\MyVideos34.sqlite
newsletter-1 c:\utilities\newsletter-1
notes.txt c:\utilities\notes.txt
README c:\utilities\README
saveHighlighted.ahk c:\utilities\saveHighlighted.ahk
saveHighlighted.ahk.bak c:\utilities\saveHighlighted.ahk.bak
temp.htm c:\utilities\temp.htm
to_csv.py c:\utilities\to_csv.py

回答by Iulian Bute

recursive solution:

递归解决方案:

sum(len(fs) for _,_,fs in os.walk(os.getcwd()))

for current directory solution:

对于当前目录解决方案:

len(os.walk(os.getcwd()).next()[2])