<h2>使用<code>pathlib</code>:</h2>
<ul>
<li><a href="https://docs.python.org/3/library/pathlib.html" rel="nofollow noreferrer">pathlib</a>部分
应使用标准库,而不是<code>os</code>。你知道吗</li>
<li><a href="https://realpython.com/python-pathlib/" rel="nofollow noreferrer">Python 3's pathlib Module: Taming the File
System</a></li>
</ul>
<h3>显然你也想要这些文件:</h3>
<pre><code>from pathlib import Path
import numpy as np
p = Path.cwd() # if you're running in the current dir
p = Path('path to to dir') # pick one
# gets all the files
f = [y for y in p.rglob(f'*')]
# counts them
values, counts = np.unique([x.parent for x in f ], return_counts=True)
print(list(zip(counts, values)))
</code></pre>
<h3>输出:</h3>
<ul>
<li>包含count和path的元组列表</li>
</ul>
<pre><code>[(8, WindowsPath('E:/PythonProjects/stack_overflow')),
(2, WindowsPath('E:/PythonProjects/stack_overflow/.ipynb_checkpoints')),
(7, WindowsPath('E:/PythonProjects/stack_overflow/complete_solutions/data')),
(3, WindowsPath('E:/PythonProjects/stack_overflow/csv_files')),
(1,
WindowsPath('E:/PythonProjects/stack_overflow/csv_files/.ipynb_checkpoints')),
(5, WindowsPath('E:/PythonProjects/stack_overflow/data'))]
</code></pre>
<ul>
<li><code>print(f)</code>将返回文件列表</li>
</ul>