写一个python脚本,检测100个txt文件里,第三行是否有“成功”两个字
时间: 2024-04-08 07:31:03 浏览: 14
当然可以!以下是一个示例的Python脚本,它可以检测100个txt文件中的第三行是否包含"成功"两个字:
```python
import os
def check_txt_files():
directory = '/path/to/txt/files' # 替换为你的txt文件所在的目录路径
file_count = 0
success_count = 0
for filename in os.listdir(directory):
if filename.endswith(".txt"):
file_count += 1
filepath = os.path.join(directory, filename)
with open(filepath, 'r') as file:
lines = file.readlines()
if len(lines) >= 3 and "成功" in lines[2]:
success_count += 1
print(f"总共检测了 {file_count} 个文件。")
print(f"其中有 {success_count} 个文件的第三行包含了\"成功\"两个字。")
check_txt_files()
```
请将`/path/to/txt/files`替换为包含100个txt文件的目录的实际路径。运行脚本后,它将输出总共检测了多少个文件以及有多少个文件的第三行包含了"成功"两个字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)