教程集 www.jiaochengji.com
教程集 >  Python编程  >  python进阶  >  正文 Python:批量重命名(图片、文本)

Python:批量重命名(图片、文本)

发布时间:2021-01-18   编辑:jiaochengji.com
教程集为您提供Python:批量重命名(图片、文本)等资源,欢迎您收藏本站,我们将为您提供最新的Python:批量重命名(图片、文本)资源

1、前言

最近在家学习深度学习的caffe,在做某类识别的时候,自己采集到的图片,命名方式很乱,不利于caffe的模型训练, 所以采用python来实现对图片或者文本数据的批量重命名。

2、基本思路

调用到 python 的 os 模块,对某文件夹下的数据进行遍历(listdir),同时使用 rename 进行重命名操作即可。

3、实现效果

fc0233ba7839525e5aca1abe3c46f02.png

4、实现代码

代码如下:

# -*- coding:utf8 -*-
#usage:实现对图片的批量重命名

import os
class BatchRename():
#定义函数执行图片的路径  
    def __init__(self):
        self.path = '/home/nvidia/caffe/data/cheb/test/aodi'
#定义函数实现重命名操作
    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 101
        for item in filelist:
            if item.endswith('.jpg'):
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), str(i)   '.jpg')
                try:
                    os.rename(src, dst)
                    print 'converting %s to %s ...' % (src, dst)
                    i = i   1
                except:
                    continue
        print ('total %d to rename & converted %d jpgs' % (total_num, i))
#主函数调用
if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

python学习网,免费的在线学习python平台,欢迎关注!

本文转自:https://blog.csdn.net/ITBigGod/article/details/79352547

您可能感兴趣的文章:
什么是批处理
Python:批量重命名(图片、文本)
批处理bat批量修改文件名的实例代码
批量追加文件后缀名的批处理脚本
python怎么一次性下载多个文件
linux命令行bash批量重命名文件
linux批量修改文件名的shell脚本
简单批处理命令简介
用Python实现批量修改文件名
Python之sys和argv详解

[关闭]
~ ~