python-[简单训练2]-搜索当前文件夹所有的.txt文件,对用户输入的正则表达式进行搜索并进行显示

el/2023/6/4 15:45:52

问题描述

打开文件夹中的所有.txt文件,
查找匹配用户提供的正则表达式的所有行,
并将结果输出在屏幕上

代码如下:

import os
import re
d = []
for filename in os.listdir(os.getcwd()):if filename.endswith('.txt'):a = os.path.join(os.getcwd(), filename)d.append(a)print(a)
print('Please input rex :')
rex = input()
for i in range(len(d)):file = open(d[i], 'r')filecontent = file.read()Regex = re.compile(rex)mo = Regex.findall(filecontent)print(mo)

输出结果:

C:\WORK\Count\mainFunction\123.txt
Please input rex :
\d
['1', '2', '3', '1', '2', '3', '1', '2', '4', '4', '1', '4']
http://www.ngui.cc/el/3419051.html

相关文章

python-[简单训练3]-搜索当前文件夹属于一类的所有文件,检查序号是否连续,不连续则修改(消除缺失的编号)

问题描述 在一个文件夹中,找到所有带指定前缀的文件,例如:spam001.txt 定位缺失的编号,让程序对后面所有的文件改名,消除缺失的编号 代码如下: import os, re, shutil cuurentdir os.getcwd() textname …

python-[openpyxl]-代码更改后

当作收藏 openpyxl模块介绍 openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读取和修改Excel文档。其他很多的与Excel相关的项目基本只…

QT-定时器QTimer 简单计时

定时器经常会用到&#xff0c;其实定时器是非常简单的&#xff0c;代码只有几个。一、创建定时器 一定要包含头文件 #include <QTimer> QTimer *Timer;然后新建即可 Timer new QTimer对定时器结束的connect connect(Timer, SIGNAL(timeout()), this, SLOT());二、一些…

python-opencv-ValueError: setting an array element with a sequence

Error描述 C:\WORK\Python\python.exe C:/Users/雪山飞狐/Desktop/研究生资料/数字图像处理/shiyan1.py Traceback (most recent call last):File "C:/Users/雪山飞狐/Desktop/研究生资料/数字图像处理/shiyan1.py", line 23, in <module>img_4 downsampling…

python-opencv-matplotlib灰度图可视化处理

参考文章1、golgotha的ttps://www.jianshu.com/p/de9013f181d7 一、Demo 一、1.热度图 利用matplotlib可视化一张图片的灰度图。 纵横坐标为图片的像素点位置(x, y),此像素点的灰度值z(x, y)当作z轴上的取值。 首先利用plot_surface分析某张图片的灰度图 import matplotli…

python-AttributeError: module 'matplotlib' has no attribute 'contourf'

错误内容 Traceback (most recent call last):File "C:/Users/雪山飞狐/Desktop/学习总结/ML100天/Code/Day_4.py", line 30, in <module>plt.contourf(X1, X2, classifer.predict(np.array([X1

(LaTex)CTex使用详细教程与资料

一份其实很短的LaTex入门文档 &#xff08;LaTex)CTex的初次使用心得及入门教程 一、 为啥子要用LaTex &#xff08;1&#xff09;待编辑文档规模尺度相关&#xff1a; 学位论文&#xff0c;书籍的编辑用LaTex就比较方便 &#xff08;2&#xff09;编号的方便&#xff1a; …

Pytorch Error:RuntimeError: Assertion cur_target 0 cur_target n_classes failed

ERROR 使用pytorch的函数 torch.nn.CrossEntropyLoss()计算Loss时报错 或者 loss criterion(output, target) 报错&#xff1a; RuntimeError: Assertion cur_target > 0 && cur_target < n_classes failed解决方法&#xff1a; 原因一&#xff1a;模型输出与…

Pytorch Error: ValueError: Expected input batch_size (324) to match target batch_size (4) Log In

ERROR 运行到loss = criterion(output, target)时 报错: ValueError: Expected input batch_size (324) to match target batch_size (4) Log In解决方法 打印 class Net(nn.Module):def __init__(se

Pytorch Erroe:invalid argument 0: Sizes of tensors must match except in dimension 0. Got 62 and 56 i

Error invalid argument 0: Sizes of tensors must match except in dimension 0. Got 62 and 56 in dimension 2 at C:\Users\builder\AppData\Local\Temp\pip-req-build-9msmi1s9\aten\src\TH/generic/THTensor.cpp:689