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装饰器相当于函数的调用方式

1. 普通装饰器 import logging 1. foo = use_loggine(foo) def use_loggine(func): def wrapper(...

pytorch + visdom 处理简单分类问题的示例

pytorch + visdom 处理简单分类问题的示例

环境 系统 : win 10 显卡:gtx965m cpu :i7-6700HQ python 3.61 pytorch 0.3 包引用 import torch from...

python实现简单购物商城

 本文为大家分享了购物商城小程序,供大家参考,具体内容如下 软件版本:python3.x 功能:实现简单购物商城 1.允许用户选择购买多少件 2.允许多用户登录,下一次登录后,...

使用Python和Prometheus跟踪天气的使用方法

开源监控系统 Prometheus 集成了跟踪多种类型的时间序列数据,但如果没有集成你想要的数据,那么很容易构建一个。一个经常使用的例子使用云端提供商的自定义集成,它使用提供商的 API...

python处理html转义字符的方法详解

本文实例讲述了python处理html转义字符的方法。分享给大家供大家参考,具体如下: 最近在用Python处理网页数据时,经常遇到一些html转义字符(也叫html字符实体),例如&l...