查看一个目录下的文件数量
在 Linux / macOS 终端中
- 只统计文件数量(不含目录):
find /path/to/dir -type f | wc -l
- 只统计目录数量:
find /path/to/dir -type d | wc -l
- 只看当前目录(不递归)下的文件数量:
find /path/to/dir -maxdepth 1 -type f | wc -l
在 Windows 命令行(CMD 或 PowerShell)
PowerShell 中统计文件数量(递归):
(Get-ChildItem -Recurse -File | Measure-Object).Count
当前目录下(不递归)文件数量:
(Get-ChildItem -File | Measure-Object).Count
Python 脚本示例(跨平台):
import osdef count_files(directory):count = 0for root, dirs, files in os.walk(directory):count += len(files)return countprint(count_files("/path/to/dir"))