查找指定目录下所有子目录文件中是否存在关键字
2024-04-23 19:25 Tanwheey 阅读(36) 评论(0) 编辑 收藏 举报1 | ## Python2.7# -*- coding: gb18030 -*-<br><br>import os<br>import io<br>import sys<br>reload(sys)<br>sys.setdefaultencoding('gb18030')<br><br>path = r'D:\k1_total_quantity_scripts\Suites\Onetrack\Design Vaildation TEST\Feature TEST\FE TEST\NVME'<br><br>for root, _, files in os.walk(path):<br> if len(files) > 0:<br> for f in files:<br> position = root + '\\' + f<br><br> with io.open(position, encoding='gb18030', errors='ignore') as ff:<br> content = ff.read()<br> lines = content.splitlines()<br> for line in lines:<br> if '0xc5' in line.strip(): # 查找含有关键字的行<br> with open('log.txt', 'a') as fff:<br> fff.write(f + ' , '+line.strip() + '\n') # 保存含有关键字的文件 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ## Python 3 #!/usr/bin/env python # -*- coding: utf-8 -*- import os import io import sys sys.stdout = io.TextIOWrapper(sys.stdout. buffer , encoding = 'utf-8' ) #改变标准输出的默认编码 path = r 'D:\NewProgram\Unionmem_SSD\TestCases\03_产品集成测试\201_ESSD特性测试\303_FE_TEST\NVME' for root, _, files in os.walk(path): if len (files) > 0 : for f in files: position = root + '\\' + f with io. open (position, encoding = 'utf-8' , errors = 'ignore' ) as ff: content = ff.read() lines = content.splitlines() for line in lines: if '0xc5' in line.strip(): # 查找含有关键字的行 with open ( 'log.txt' , 'a' ) as fff: fff.write(f + ' , ' + line.strip() + '\n' ) # 保存含有关键字的文件 |
Python3环境下,生成TXT文件后,在文件中选择“以GBK重新加载”显示文件。且文件设置中的文件编码中的全局编码、项目编码、属性文件默认编码都设置成utf-8
编码名称 | 用途 |
utf8 | 所有语言 |
gbk | 简体中文 |
gb2312 | 简体中文 |
gb18030 | 简体中文 |
big5 | 繁体中文 |
big5hkscs | 繁体中文 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2021-04-23 使用 Cygwin 在 Windows 中使用 Linux 命令