Python实现主文件夹内子文件夹的遍历与特定字符串匹配 遍历指定主文件夹及其所有子文件夹。 查找每个子文件夹中的 CommonCollectResult.txt 文件。 在找到的文件中搜索匹配特定字符串的行。 按照子文件夹名称对匹配的结果进行分类输出。
时间: 2025-06-24 10:38:16 浏览: 17
### 实现方案
为了实现这一需求,可以采用 `os` 模块或者更现代的 `pathlib` 模块来遍历文件夹及其子文件夹。以下是基于两种方法的具体实现:
#### 方法一:使用 `os.walk`
通过 `os.walk` 可以递归地访问主文件夹下的所有子文件夹和文件。对于每个找到的文件,如果其名为 `CommonCollectResult.txt`,则打开该文件并逐行读取内容,寻找匹配特定字符串的行。
```python
import os
def search_in_subfolders(root_folder, target_file_name, match_string):
result = {}
for root, _, files in os.walk(root_folder): # 遍历根目录及所有子目录
if target_file_name in files: # 如果目标文件存在于当前目录中
file_path = os.path.join(root, target_file_name)
with open(file_path, 'r', encoding='utf-8') as f: # 打开文件
lines = []
for line_number, line_content in enumerate(f, start=1): # 逐行读取
if match_string in line_content: # 如果某一行包含指定字符串
lines.append((line_number, line_content.strip())) # 记录行号和内容
if lines: # 如果有匹配项,则按子文件夹分类存储
subfolder_name = os.path.basename(root) # 获取子文件夹名
result[subfolder_name] = lines
return result
root_directory = "./example_root" # 主文件夹路径
target_filename = "CommonCollectResult.txt"
search_string = "specific_text"
output_results = search_in_subfolders(root_directory, target_filename, search_string)
for folder, matches in output_results.items():
print(f"\nSubfolder: {folder}")
for line_num, content in matches:
print(f"Line {line_num}: {content}")
```
此代码片段实现了按照子文件夹名称分类的结果输出功能[^1]。
---
#### 方法二:使用 `pathlib`
相较于传统的 `os` 模块,`pathlib` 提供了一种更加面向对象的方式来处理文件系统操作。下面是一个类似的解决方案:
```python
from pathlib import Path
def search_with_pathlib(root_dir, target_file_name, match_string):
results = {}
for path in Path(root_dir).rglob(target_file_name): # 使用 rglob 进行递归查找
subfolder_name = path.parent.name
with path.open('r', encoding='utf-8') as file:
matching_lines = [
(idx, line.strip())
for idx, line in enumerate(file, start=1)
if match_string in line
]
if matching_lines:
results[subfolder_name] = matching_lines
return results
root_directory = "./example_root" # 主文件夹路径
target_filename = "CommonCollectResult.txt"
search_string = "specific_text"
output_results = search_with_pathlib(root_directory, target_filename, search_string)
for folder, matches in output_results.items():
print(f"\nSubfolder: {folder}")
for line_num, content in matches:
print(f"Line {line_num}: {content}")
```
这段代码同样能够满足题目要求,并且利用了现代化库的功能[^3]。
---
### 注意事项
1. **编码问题**:确保所读取的文本文件使用的字符集与程序设定一致(通常为 UTF-8)。如果不一致可能会引发解码错误。
2. **性能优化**:当面对非常庞大的数据量时,考虑分批次加载或异步执行可能有助于提升效率。
3. **异常捕获**:实际应用中建议加入更多的健壮性设计,比如对无法正常读写的文件进行跳过处理等。
---
### 输出示例
假设存在如下结构的数据源:
```
/example_root/folderA/CommonCollectResult.txt -> 包含 "specific_text" 的若干行
/example_root/folderB/CommonCollectResult.txt -> 不包含任何相关内容
/example_root/folderC/CommonCollectResult.txt -> 包含另一些带有 "specific_text" 的记录
```
运行以上任一段脚本后会得到类似这样的终端打印结果:
```
Subfolder: folderA
Line 5: This is a specific_text example.
Line 10: Another instance of the specific_text.
Subfolder: folderC
Line 7: Yet another occurrence of specific_text here.
```
---
阅读全文
相关推荐













