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

el/2023/9/24 22:17:28

问题描述

在一个文件夹中,找到所有带指定前缀的文件,例如:spam001.txt
定位缺失的编号,让程序对后面所有的文件改名,消除缺失的编号

代码如下:

import os, re, shutil
cuurentdir = os.getcwd()
textname = []  #储存符合要求的文件名称
for filename in os.listdir('.'):mo = re.compile(r'spam\d\d\d\.txt').search(filename)if mo != None:textname.append(filename)
number = 1
# 对该类的所有文件进行循环,检查是否需要修改
for i in range(len(textname)):all = os.path.join(cuurentdir,textname[i])mo = re.compile(r'(\d)+').search(textname[i])if int(mo.group()) != number:end = os.path.join(cuurentdir,'spam'+str(number).rjust(3,'0')+'.txt')shutil.move(all,end)print('Success revise %s to %s' % (all , end))number += 1

结果如下:

C:\WORK\Python\python.exe C:/WORK/Count/mainFunction/ceshi.py
Success revise C:\WORK\Count\mainFunction\spam013.txt to C:\WORK\Count\mainFunction\spam003.txt
Success revise C:\WORK\Count\mainFunction\spam028.txt to C:\WORK\Count\mainFunction\spam004.txt

http://www.ngui.cc/el/3419050.html

相关文章

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

Pytorch Error:RuntimeError:CUDA error: out of memory [in torch.load]

Error: 当加载模型时&#xff1a; model.load_state_dict(torch.load(xxx.pth))Error:RuntimeError:CUDA error: out of memory解决方法 原因1-CUDA显存不足 解决办法&#xff1a;换成显存更大的显卡 原因2-显卡被占用 当存在多个显卡时&#xff0c;pytorch默认使用0号显…