# -*- coding: utf-8 -*-
import os
path = "H:\\test_datasets\\zxwdata"
filelist = os.listdir(path) #该文件夹下所有的文件(包括文件夹)
count=1
for file in filelist:
    print(file)
for file in filelist:   #遍历所有文件
    Olddir=os.path.join(path,file)   #原来的文件路径
    if os.path.isdir(Olddir):   #如果是文件夹则跳过
        continue
    filename=os.path.splitext(file)[0]   #文件名
    filetype=os.path.splitext(file)[1]   #文件扩展名
    filename="zxw"
    Newdir=os.path.join(path,filename+str(count).zfill(4)+filetype)  #用字符串函数zfill 以0补全所需位数
    os.rename(Olddir,Newdir)#重命名
    count+=1

python重命名

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

import os

class BatchRename():
    
    def __init__(self):
        self.path = 'H:\\test_datasets\\zxwdata'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 1
        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重命名

# coding:utf8
import os
# 目标文件夹图像路径
path='H:/zxwTest/test'
#返回指定的文件夹包含的文件或文件夹的名字的列表
filelist=os.listdir(path)
j=0
for i in filelist:
    # 判断该路径下的文件是否为图片
    if i.endswith('.jpeg'):
        # 打开图片,将多个路径组合后返回
        src=os.path.join(os.path.abspath(path),i)
        # 图像路径 + a + 右对齐且字符串长度为3 +.jpg
        dst=os.path.join(os.path.abspath(path),'a'+format(str(j),'0>3s')+'.jpg')
        # 重命名函数
        os.rename(src,dst)
        j+=1

 

相关文章:

  • 2021-10-16
  • 2021-06-15
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-11-27
猜你喜欢
  • 2022-01-01
  • 2021-06-17
  • 2021-11-16
  • 2022-12-23
  • 2021-05-29
  • 2021-06-18
相关资源
相似解决方案