批量重命名当前文件夹下所有JPG和PNG文件
Talk is cheap, show me code
import os
import sys
filelist = os.listdir('.')
# 匹配 " *.jpg " 和 " *.png " 文件
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!")
用法
- 复制粘贴到文件夹中
- 在cmd中输入“python rename.py
网友评论