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设计】。

相关文章

pytorch中torch.max和Tensor.view函数用法详解

torch.max() 1. torch.max()简单来说是返回一个tensor中的最大值。 例如: >>> si=torch.randn(4,5) >&g...

Python对象中__del__方法起作用的条件详解

对象的__del__是对象在被gc消除回收的时候起作用的一个方法,它的执行一般也就意味着对象不能够继续引用。 示范代码如下: class Demo: def __del__(sel...

使用python对excle和json互相转换的示例

python 版本:2.7 只是读取excel的话可以直接使用xlrd 1、excle to json 代码如下 # -*-coding:utf8 -*- import xlrd f...

解决phantomjs截图失败,phantom.exit位置的问题

刚刚学习使用phantomjs,根据网上帖子自己手动改了一个延时截图功能,发现延时功能就是不能执行,最后一点点排查出了问题。 看代码: var page = require('web...

Python如何实现动态数组

Python如何实现动态数组

Python序列类型 在本博客中,我们将学习探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)。 不知道你发现没有...