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+树莓派+YOLO打造一款人工智能照相机

Python+树莓派+YOLO打造一款人工智能照相机

不久之前,亚马逊刚刚推出了DeepLens 。这是一款专门面向开发人员的全球首个支持深度学习的摄像机,它所使用的机器学习算法不仅可以检测物体活动和面部表情,而且还可以检测类似弹吉他等复杂...

python编程之requests在网络请求中添加cookies参数方法详解

哎,好久没有学习爬虫了,现在想要重新拾起来。发现之前学习爬虫有些粗糙,竟然连requests中添加cookies都没有掌握,惭愧。废话不宜多,直接上内容。 我们平时使用requests...

在Python的Flask框架下使用sqlalchemy库的简单教程

flask中的sqlalchemy 相比于sqlalchemy封装的更加彻底一些 , 在一些方法上更简单 首先import类库: 在CODE上查看代码片派生到我的代码片 <...

python3编写ThinkPHP命令执行Getshell的方法

python3编写ThinkPHP命令执行Getshell的方法

加了三个验证漏洞以及四个getshell方法 # /usr/bin/env python3 # -*- coding: utf-8 -*- # @Author: Morker #...

浅析python继承与多重继承

记住以下几点: 直接子类化内置类型(如dict,list或str)容易出错,因为内置类型的方法通常会忽略用户覆盖的方法,不要子类化内置类型,用户自定义的类应该继承collections模...