Python计数目录中的文件及其所有子目录
我正在尝试计算文件夹及其所有子文件夹中的所有文件 对于典范,如果我的文件夹看起来像这样:
file1.txt
subfolder1/
├── file2.txt
├── subfolder2/
│ ├── file3.txt
│ ├── file4.txt
│ └── subfolder3/
│ └── file5.txt
└── file6.txt
file7.txt
我想获得数字7。
我尝试的第一件事是递归功能,它计算所有文件并以
def get_file_count(directory: str) -> int:
count = 0
for filename in os.listdir(directory):
file = (os.path.join(directory, filename))
if os.path.isfile(file):
count += 1
elif os.path.isdir(file):
count += get_file_count(file)
return count
这种方式为每个文件夹呼唤自己,但要花很多时间才能用于大目录。
我还记得这篇文章,它显示了一种使用win32com来计算文件夹总尺寸的快速方法图书馆还提供了一种方法来完成我想要的事情。 但是在搜索后,我只找到了这个,
fso = com.Dispatch("Scripting.FileSystemObject")
folder = fso.GetFolder(".")
size = folder.Files.Count
但这仅返回仅在目标文件夹中(而不是在其子文件夹中)中的文件数
,那么您是否知道Python中是否有最佳函数可以返回文件夹中的文件数量及其所有子文件夹?
I am trying to count all the files in a folder and all its subfolders
For exemple, if my folder looks like this:
file1.txt
subfolder1/
├── file2.txt
├── subfolder2/
│ ├── file3.txt
│ ├── file4.txt
│ └── subfolder3/
│ └── file5.txt
└── file6.txt
file7.txt
I would like get the number 7.
The first thing I tried is a recursive function who count all files and calls itself for each folder
def get_file_count(directory: str) -> int:
count = 0
for filename in os.listdir(directory):
file = (os.path.join(directory, filename))
if os.path.isfile(file):
count += 1
elif os.path.isdir(file):
count += get_file_count(file)
return count
This way works but takes a lot of time for big directories.
I also remembered this post, which shows a quick way to count the total size of a folder using win32com and I wondered if this librairy also offered a way to do what I was looking for.
But after searching, I only found this
fso = com.Dispatch("Scripting.FileSystemObject")
folder = fso.GetFolder(".")
size = folder.Files.Count
But this only returns the number of files in only the targeted folder (and not in its subfolders)
So, do you know if there is an optimal function in python that returns the number of files in a folder and all its subfolders?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
iiuc,您可以做的
或也许是为了避免
len
以获得更好的性能:IIUC, you can just do
or perhaps, to avoid the
len
for probably slightly better performance:使用库
OS
和路径
的另一个解决方案:Another solution using the libraries
os
andPath
:该代码将揭示指定根的所有目录条目(例如,普通文件,符号链接)的所有目录条目的计数。
包括时间安排和测试中使用的实际路径名:
输出:
This code will reveal a count of all directory entries that are not directories (e.g., plain files, symlinks) from a specified root.
Includes timing and an actual pathname used in the test:
Output:
我使用了os.walk()
是我的样本,希望它对您有帮助
i used os.walk()
its my sample , i hope it'll helps you
您也可以直接使用该命令:
这返回所有文件的计数
使用
OS.System()
这可以从Python完成。you could also directly use the command:
this returns the count of all files
With
os.system()
this can be done from python.正如其他人所指出的那样,正确的方法是使用
os.walk
,但要提供另一个解决方案,它尽可能类似于您的原始方法:您可以使用
os.scandir
为了避免构建整个列表的成本,应该是更快的速度:The proper way is to use
os.walk
as others have pointed out, but to give another solution which resembles your original as much as possible:You can use
os.scandir
to avoid the cost of constructing the entire list, it should be substantially faster:这是另一种方式。
考虑以下目录结构
来计数每个目录中的文本文件(即以.txt结尾的文件),
以查看这些文件是什么,
的总数
以获取以.ipynb(ipython Notebook文件)结尾的文件
,以计数所有 文件。
与从树命令计数的文件计数匹配的
Here is another way.
Consider the following directory structure
To count the text files (i.e. those ending in .txt) in each directory
To see what those files are
To get the total number of files
To count files ending with .ipynb (ipython notebook files)
To count all the files
which matches with the file count from the tree command.