python 实现对文件夹中的图像连续重命名方法

yipeiwu_com6年前Python基础

python实现的对文件夹中的图像进行连续的重命名方法:

import os

class BatchRename():
 def __init__(self):
  self.path = 'C:/Users/zxl/Desktop/tr'

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

以上这篇python 实现对文件夹中的图像连续重命名方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python任意字符串转16, 32, 64进制的方法

Python字符串转数字 import binascii s = 'test123456test' str_16 = binascii.b2a_hex(s.encode...

Python实现批量下载图片的方法

本文实例讲述了Python实现批量下载图片的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python #-*-coding:utf-8-*-' #F...

Python多进程池 multiprocessing Pool用法示例

本文实例讲述了Python多进程池 multiprocessing Pool用法。分享给大家供大家参考,具体如下: 1. 背景 由于需要写python程序, 定时、大量发送htttp请求...

Python实现的tcp端口检测操作示例

本文实例讲述了Python实现的tcp端口检测操作。分享给大家供大家参考,具体如下: # coding=utf-8 import sys import socket import r...

Python 中的with关键字使用详解

在 Python 2.5 中, with 关键字被加入。它将常用的 try ... except ... finally ... 模式很方便的被复用。看一个最经典的例子: with...