读取一个文件夹下所有图片的名称,提取文件名称部分str转为int类型,奇数+1,偶数-1,重新转为长度为7的str,生成文件名如图
# coding: utf-8
import os
import sys
path = '/home/lcj/CCTV5/20190408/DI/DI010/第四集/'
print(path)
# 打开文件
dirs = os.listdir(path)
# 遍历更名
print('[*]rename...')
for file in dirs:
# print ()
# print ('oldname:', file)
# front & extension name of flies
f,e = os.path.splitext(file)
# print ('old_index:', f)
# str to int
i = int(f)
# 奇+偶-
if (i % 2) == 1:
i += 1
else:
i -= 1
# int to str%07d
s = str("%07d"%i)
# print('new_index:', s)
rename = s + '.png'
# print('new_name:', rename)
# print (type(file))
oldname = path + file
newname = path + rename
os.rename(oldname, newname)
# print ('oldfile:', oldname)
# print ('newname:', newname)
print('rename success.')