• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞java代码 3年前 (2022-05-24) 42次浏览 已收录 0个评论

1、前言

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

2、基本思路

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

3、实现效果

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()

www#gaodaima.com来源gaodai#ma#com搞*!代#%^码$网搞代码

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

本文转自:https://www.gaodaima.com/ITBigGod/article/details/79352547


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python:批量重命名(图片、文本)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址