python实现图片文件批量重命名

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现文件批量重命名的具体代码,供大家参考,具体内容如下

代码:

# -*- coding:utf-8 -*-

import os

class ImageRename():
 def __init__(self):
  self.path = 'D:/xpu/paper/plate_data'

 def rename(self):
  filelist = os.listdir(self.path)
  total_num = len(filelist)

  i = 0

  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), '0000' + format(str(i), '0>3s') + '.jpg')
    os.rename(src, dst)
    print 'converting %s to %s ...' % (src, dst)
    i = i + 1
  print 'total %d to rename & converted %d jpgs' % (total_num, i)

if __name__ == '__main__':
 newname = ImageRename()
 newname.rename()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python编程实现的图片识别功能示例

本文实例讲述了Python编程实现的图片识别功能。分享给大家供大家参考,具体如下: 1. 安装PIL,官方没有WIN64位,Pillow替代 pip install Pillow-2.7...

python strip() 函数和 split() 函数的详解及实例

 python strip() 函数和 split() 函数的详解及实例 一直以来都分不清楚strip和split的功能,实际上strip是删除的意思;而split则是分割的意...

Python中GIL的使用详解

1、GIL简介 GIL的全称为Global Interpreter Lock,全局解释器锁。 1.1 GIL设计理念与限制 python的代码执行由python虚拟机(也叫解释器主循...

python求最大连续子数组的和

抛出问题: 求一数组如 l = [0, 1, 2, 3, -4, 5, -6],求该数组的最大连续子数组的和 如结果为[0,1,2,3,-4,5] 的和为7 问题分析: 这个问题很...

Python Celery多队列配置代码实例

这篇文章主要介绍了Python Celery多队列配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Celery官方文档 项...