Python batch rename sequence images and other functions

1Batch rename

Under python, move the sequence image to another path and renumber it starting from 1.

#coding=utf-8
#将jpg复制并重命名
import os,shutil
path_src = '/media/SrcImages/'
path_dst = '/media/DstImages/'
n = os.listdir(path_src)
len0 = len(n)
#print(len0)
bias = 13579#起始序列
ends = 34862#结束序列
cnt = 1
inds = [i+bias for i in range(ends-bias)]
for i in inds:
    file_src = path_src + 'image{:08d}.jpg'.format(i)#str(i) + '.jpg'
    print(file_src)
    file_dst = path_dst + str(cnt) + '.jpg'
    if os.path.exists(file_src):#判断文件是否存在,以防中间序列不连续
        shutil.copyfile(file_src,file_dst)
        cnt = cnt +1
    

2 Create folder

#coding=utf-8
#创建文件夹
import os
paths = ['dir1','dir2','dir3']
for path_i in paths:
    if not os.path.exists(path_i):
        os.makedirs(path_i)

Guess you like

Origin blog.csdn.net/alansss/article/details/119191058