前面我们实现了一个遍历一个目录所有txt文件,将匹配的文件名和行内容保存在文件中。
【Class 36】第七章 实例 python实现正则表达式查找内容
今个儿,我们给前面这道题,稍微加强一下,用Python 来实现 Linux grep 命令
grep命令分析:
- 输入: 无论输入字符串 还是 正则表达式都能搜索
- 搜索: 能够遍历目录及子目录下的所有文件 (我们此处暂时先简化为 遍历目录及子目录下的所有文本文件)
===> 此处需实现递归目录读取,以及 文本匹配成功后,将内容append在list中 - 结果: 搜索结果保存在 ./result/search_result.txt 中
第一步 生成数据文件
实现之前,想着,我即便写好了上面的程序,也没有东西来供我查找,
所以打算先用递归生成一些目录和txt文件,来供我后续使用
grep 的命令,明天来开始实现
实现源码
#! python3
# -*- coding: utf-8 -*-
import re, os , time, shutil
# 在当前目录生成5个目录,每个目录中有不同名字的5个文件,树状循环两次
# 内容为 this is xxx文件名 \n 生成时间为当前时间: 2019/3/4-21:52:20
def generate_file(dir_name_='.\\test_dir', file_depth=0 , dir_depth=0 , dir_cnt_=0):
# 判断目录深度
if dir_depth >= 3:
dir_depth = 0
return None
dir_depth += 1
# print(dir_name_)
# 创建第一级目录
if dir_name_=='.\\test_dir' and os.path.exists(dir_name_):
shutil.rmtree(dir_name_)
os.makedirs(dir_name_)
# 创建五个txt文件
for k in range(5):
file_name=''
file_name = "%s\\Test_File_%d.txt"%(dir_name_, file_depth + k)
# 创建5个文件
fp = open(file_name, 'w')
# 内容写入
fp.write("This is %s !\nin directory: %s\nCurrent Time is %s"%( os.path.basename(file_name),dir_name_, time.strftime('%Y/%m/%d-%H:%M:%S')))
fp.close()
# 递归调用目录
for k in range(5):
# 文件命名起点增加5
file_depth += 5
# 目录名字计数计算
dir_cnt = (dir_depth-1)*5 + dir_cnt_*5 + k if dir_depth > 1 else (dir_depth-1)*5 + k
# 递归调用
generate_file("%s\\sub_dir_%d_%d"%(dir_name_,dir_depth , dir_cnt), file_depth, dir_depth , k)
if __name__ == "__main__":
generate_file()
运行结果
目录结构如下:
PS C:\Users\Administrator\Desktop\tmp\test> tree .
文件夹 PATH 列表
C:\USERS\ADMINISTRATOR\DESKTOP\TMP\TEST
└─test_dir
├─sub_dir_1_0
│ ├─sub_dir_2_5
│ ├─sub_dir_2_6
│ ├─sub_dir_2_7
│ ├─sub_dir_2_8
│ └─sub_dir_2_9
├─sub_dir_1_1
│ ├─sub_dir_2_10
│ ├─sub_dir_2_11
│ ├─sub_dir_2_12
│ ├─sub_dir_2_13
│ └─sub_dir_2_14
├─sub_dir_1_2
│ ├─sub_dir_2_15
│ ├─sub_dir_2_16
│ ├─sub_dir_2_17
│ ├─sub_dir_2_18
│ └─sub_dir_2_19
├─sub_dir_1_3
│ ├─sub_dir_2_20
│ ├─sub_dir_2_21
│ ├─sub_dir_2_22
│ ├─sub_dir_2_23
│ └─sub_dir_2_24
└─sub_dir_1_4
├─sub_dir_2_25
├─sub_dir_2_26
├─sub_dir_2_27
├─sub_dir_2_28
└─sub_dir_2_29
PS C:\Users\Administrator\Desktop\tmp\test>
文本内容如下:
This is Test_File_2.txt !
in directory: .\test_dir
Current Time is 2019/03/04-23:39:31
This is Test_File_17.txt !
in directory: .\test_dir\sub_dir_1_2
Current Time is 2019/03/04-23:39:31
This is Test_File_42.txt !
in directory: .\test_dir\sub_dir_1_4\sub_dir_2_27
Current Time is 2019/03/04-23:39:32
txt 文本结构如下:
PS C:\Users\Administrator\Desktop\tmp\test> tree /F .
文件夹 PATH 列表
C:\USERS\ADMINISTRATOR\DESKTOP\TMP\TEST
│ grep_str.py
│
└─test_dir
│ Test_File_0.txt
│ Test_File_1.txt
│ Test_File_2.txt
│ Test_File_3.txt
│ Test_File_4.txt
│
├─sub_dir_1_0
│ │ Test_File_5.txt
│ │ Test_File_6.txt
│ │ Test_File_7.txt
│ │ Test_File_8.txt
│ │ Test_File_9.txt
│ │
│ ├─sub_dir_2_5
│ │ Test_File_10.txt
│ │ Test_File_11.txt
│ │ Test_File_12.txt
│ │ Test_File_13.txt
│ │ Test_File_14.txt
│ │
│ ├─sub_dir_2_6
│ │ Test_File_15.txt
│ │ Test_File_16.txt
│ │ Test_File_17.txt
│ │ Test_File_18.txt
│ │ Test_File_19.txt
│ │
│ ├─sub_dir_2_7
│ │ Test_File_20.txt
│ │ Test_File_21.txt
│ │ Test_File_22.txt
│ │ Test_File_23.txt
│ │ Test_File_24.txt
│ │
│ ├─sub_dir_2_8
│ │ Test_File_25.txt
│ │ Test_File_26.txt
│ │ Test_File_27.txt
│ │ Test_File_28.txt
│ │ Test_File_29.txt
│ │
│ └─sub_dir_2_9
│ Test_File_30.txt
│ Test_File_31.txt
│ Test_File_32.txt
│ Test_File_33.txt
│ Test_File_34.txt
│
├─sub_dir_1_1
│ │ Test_File_10.txt
│ │ Test_File_11.txt
│ │ Test_File_12.txt
│ │ Test_File_13.txt
│ │ Test_File_14.txt
│ │
│ ├─sub_dir_2_10
│ │ Test_File_15.txt
│ │ Test_File_16.txt
│ │ Test_File_17.txt
│ │ Test_File_18.txt
│ │ Test_File_19.txt
│ │
│ ├─sub_dir_2_11
│ │ Test_File_20.txt
│ │ Test_File_21.txt
│ │ Test_File_22.txt
│ │ Test_File_23.txt
│ │ Test_File_24.txt
│ │
│ ├─sub_dir_2_12
│ │ Test_File_25.txt
│ │ Test_File_26.txt
│ │ Test_File_27.txt
│ │ Test_File_28.txt
│ │ Test_File_29.txt
│ │
│ ├─sub_dir_2_13
│ │ Test_File_30.txt
│ │ Test_File_31.txt
│ │ Test_File_32.txt
│ │ Test_File_33.txt
│ │ Test_File_34.txt
│ │
│ └─sub_dir_2_14
│ Test_File_35.txt
│ Test_File_36.txt
│ Test_File_37.txt
│ Test_File_38.txt
│ Test_File_39.txt
│
├─sub_dir_1_2
│ │ Test_File_15.txt
│ │ Test_File_16.txt
│ │ Test_File_17.txt
│ │ Test_File_18.txt
│ │ Test_File_19.txt
│ │
│ ├─sub_dir_2_15
│ │ Test_File_20.txt
│ │ Test_File_21.txt
│ │ Test_File_22.txt
│ │ Test_File_23.txt
│ │ Test_File_24.txt
│ │
│ ├─sub_dir_2_16
│ │ Test_File_25.txt
│ │ Test_File_26.txt
│ │ Test_File_27.txt
│ │ Test_File_28.txt
│ │ Test_File_29.txt
│ │
│ ├─sub_dir_2_17
│ │ Test_File_30.txt
│ │ Test_File_31.txt
│ │ Test_File_32.txt
│ │ Test_File_33.txt
│ │ Test_File_34.txt
│ │
│ ├─sub_dir_2_18
│ │ Test_File_35.txt
│ │ Test_File_36.txt
│ │ Test_File_37.txt
│ │ Test_File_38.txt
│ │ Test_File_39.txt
│ │
│ └─sub_dir_2_19
│ Test_File_40.txt
│ Test_File_41.txt
│ Test_File_42.txt
│ Test_File_43.txt
│ Test_File_44.txt
│
├─sub_dir_1_3
│ │ Test_File_20.txt
│ │ Test_File_21.txt
│ │ Test_File_22.txt
│ │ Test_File_23.txt
│ │ Test_File_24.txt
│ │
│ ├─sub_dir_2_20
│ │ Test_File_25.txt
│ │ Test_File_26.txt
│ │ Test_File_27.txt
│ │ Test_File_28.txt
│ │ Test_File_29.txt
│ │
│ ├─sub_dir_2_21
│ │ Test_File_30.txt
│ │ Test_File_31.txt
│ │ Test_File_32.txt
│ │ Test_File_33.txt
│ │ Test_File_34.txt
│ │
│ ├─sub_dir_2_22
│ │ Test_File_35.txt
│ │ Test_File_36.txt
│ │ Test_File_37.txt
│ │ Test_File_38.txt
│ │ Test_File_39.txt
│ │
│ ├─sub_dir_2_23
│ │ Test_File_40.txt
│ │ Test_File_41.txt
│ │ Test_File_42.txt
│ │ Test_File_43.txt
│ │ Test_File_44.txt
│ │
│ └─sub_dir_2_24
│ Test_File_45.txt
│ Test_File_46.txt
│ Test_File_47.txt
│ Test_File_48.txt
│ Test_File_49.txt
│
└─sub_dir_1_4
│ Test_File_25.txt
│ Test_File_26.txt
│ Test_File_27.txt
│ Test_File_28.txt
│ Test_File_29.txt
│
├─sub_dir_2_25
│ Test_File_30.txt
│ Test_File_31.txt
│ Test_File_32.txt
│ Test_File_33.txt
│ Test_File_34.txt
│
├─sub_dir_2_26
│ Test_File_35.txt
│ Test_File_36.txt
│ Test_File_37.txt
│ Test_File_38.txt
│ Test_File_39.txt
│
├─sub_dir_2_27
│ Test_File_40.txt
│ Test_File_41.txt
│ Test_File_42.txt
│ Test_File_43.txt
│ Test_File_44.txt
│
├─sub_dir_2_28
│ Test_File_45.txt
│ Test_File_46.txt
│ Test_File_47.txt
│ Test_File_48.txt
│ Test_File_49.txt
│
└─sub_dir_2_29
Test_File_50.txt
Test_File_51.txt
Test_File_52.txt
Test_File_53.txt
Test_File_54.txt
PS C:\Users\Administrator\Desktop\tmp\test>