遍历目录并统计文件夹信息的 Python 脚本
在处理大量文件和文件夹的时候,经常需要对它们进行分类、统计和整理。Python 的 os 模块提供了一些非常有用的函数,可以方便地遍历目录和获取文件信息。本文将介绍如何使用 Python 脚本遍历指定目录下以特定字符开头的文件夹,并统计每个文件夹包含的子文件夹数量和子文件夹名称列表,最终将结果保存到 Excel 文件中。
1. 导入模块
首先,我们需要导入 os 和 openpyxl 模块。os 模块提供了许多处理文件和目录的函数,openpyxl 模块则是一个用于读写 Excel 文件的库。
```
import os
from openpyxl import Workbook
```
2. 设置目录和前缀
接下来,我们需要定义要遍历的目录路径和以哪个字符开头的文件夹名称。这里我们将目录路径设置为 D:/项目,将前缀设置为 2023。
```
root_dir = 'D:/项目'
prefix = '2023'
```
3. 创建 Excel 工作簿
然后,我们创建一个新的 Excel 工作簿,并获取默认工作表。我们还可以通过 wb.create_sheet() 方法创建新的工作表。这里我们只使用默认工作表。
```
wb = Workbook()
ws = wb.active
```
4. 设置表头
接下来,我们设置 Excel 表头。这里我们将列 A 设置为“文件夹名字”,列 B 设置为“里面的文件夹的数量”,列 C 设置为“包含的文件夹名字列表”。
```
ws['A1'] = '文件夹名字'
ws['B1'] = '里面的文件夹的数量'
ws['C1'] = '包含的文件夹名字列表'
```
5. 遍历目录并处理每个文件夹
现在,我们开始遍历目录,并处理每个文件夹。我们只处理以指定字符开头的文件夹,并统计每个文件夹下的子文件夹数量和子文件夹名称列表。这里我们使用 os.walk() 函数遍历目录,并使用 os.path.basename() 函数获取当前目录名称。
```
for dirpath, dirnames, filenames in os.walk(root_dir):
if os.path.basename(dirpath).startswith(prefix):
subdirs = [name for name in dirnames if not name.startswith('.')]
subdir_count = len(subdirs)
print('{}: {} {{'.format(dirpath, subdir_count))
for subdir in subdirs:
subdir_path = os.path.join(dirpath, subdir)
subdir_subdirs = [name for name in os.listdir(subdir_path) if os.path.isdir(os.path.join(subdir_path, name)) and not name.startswith('.')]
print('\t{}: {}'.format(subdir, len(subdir_subdirs)))
print('}')
```
6. 将数据写入 Excel 文件
遍历完目录后,我们将统计结果写入到 Excel 文件中。我们使用一个变量 row 记录当前行数,并在每次循环中增加它。对于每个以指定字符开头的文件夹,我们获取其子文件夹数量和子文件夹名称列表,并将它们写入到当前行中。
```
row = 2
for dirpath, dirnames, filenames in os.walk(root_dir):
if os.path.basename(dirpath).startswith(prefix):
subdirs = [name for name in dirnames if not name.startswith('.')]
subdir_count = len(subdirs)
subdir_names = ', '.join(subdirs)
ws.cell(row=row, column=1, value=dirpath)
ws.cell(row=row, column=2, value=subdir_count)
ws.cell(row=row, column=3, value='{' + subdir_names + '}')
row += 1
```
7. 保存 Excel 文件
最后,我们将 Excel 文件保存到指定位置。这里我们将其保存到 E:/工作/脚本/python/2023.xlsx。
```
save_path = 'E:/工作/脚本/python/2023.xlsx'
wb.save(save_path)
```
完整代码如下:
```
import os
from openpyxl import Workbook
# 定义要遍历的目录路径和以哪个字符开头的文件夹名称
root_dir = 'D:/项目'
prefix = '2023'
# 创建一个新的 Excel 工作簿
wb = Workbook()
# 获取默认工作表(也可以通过 wb.create_sheet() 创建新的工作表)
ws = wb.active
# 设置表头
ws['A1'] = '文件夹名字'
ws['B1'] = '里面的文件夹的数量'
ws['C1'] = '包含的文件夹名字列表'
# 遍历目录并处理每个文件夹
for dirpath, dirnames, filenames in os.walk(root_dir):
# 只处理以指定字符开头的文件夹
if os.path.basename(dirpath).startswith(prefix):
# 统计该文件夹下的子文件夹数量
subdirs = [name for name in dirnames if not name.startswith('.')]
subdir_count = len(subdirs)
# 打印结果
print('{}: {} {{'.format(dirpath, subdir_count))
for subdir in subdirs:
subdir_path = os.path.join(dirpath, subdir)
subdir_subdirs = [name for name in os.listdir(subdir_path) if os.path.isdir(os.path.join(subdir_path, name)) and not name.startswith('.')]
print('\t{}: {}'.format(subdir, len(subdir_subdirs)))
print('}')
# 遍历目录并处理每个文件夹
row = 2
for dirpath, dirnames, filenames in os.walk(root_dir):
# 只处理以指定字符开头的文件夹
if os.path.basename(dirpath).startswith(prefix):
# 统计该文件夹下的子文件夹数量
subdirs = [name for name in dirnames if not name.startswith('.')]
subdir_count = len(subdirs)
# 获取包含的文件夹名字列表
subdir_names = ', '.join(subdirs)
# 将数据写入到当前行
ws.cell(row=row, column=1, value=dirpath)
ws.cell(row=row, column=2, value=subdir_count)
ws.cell(row=row, column=3, value='{' + subdir_names + '}')
# 增加行数
row += 1
# 保存 Excel 文件
# wb.save('example.xlsx')
# 自定义 Excel 文件的保存位置
save_path = 'E:/工作/脚本/python/2023.xlsx'
# 保存 Excel 文件
wb.save(save_path)
```
以上就是本文介绍的 Python 脚本遍历指定目录并统计文件夹信息的方法。这个脚本可以帮助你快速地了解一个目录下所有以特定字符开头的文件夹包含哪些子文件夹,以及它们各自包含了多少个子文件夹。如果你需要对大量的数据进行分类、整理和统计,Python 脚本将是一个非常有用的工具。