【Python学习笔记】批量重命名图片文件

以前写的一个小脚本

'''
功能:批量重命名当前文件夹下的jpg和png文件
'''

import os
import sys

filelist = os.listdir('.')
jpgfile = [name for name in filelist if name.endswith(('.jpg','.JPG'))]
pngfile = [name for name in filelist if name.endswith(('.png','.PNG'))]

def jpgrename():
    for i in range(len(jpgfile)):
        newname = "JPG" + str(i+1) + ".jpg"    
        os.rename(jpgfile[i],newname)

def pngrename():
    for i in range(len(pngfile)):
        newname = "PNG" + str(i+1) + ".png"    
        os.rename(pngfile[i],newname)

if __name__ == '__main__':
    jpgrename()
    pngrename()
    sys.stdin.flush()
    print("finish!")