文件夹内图片批量重命名代码

    技术2022-07-12  68

    文件夹内图片批量重命名代码

    import os import re import sys import cv2 import torchvision.transforms as transforms path = r"data/masks/" def renameall(path): fileList = os.listdir(path) # 待修改文件夹 currentpath = os.getcwd() # 得到进程当前工作目录 os.chdir(path) for fileName in fileList: # 遍历文件夹中所有文件 filenamenew = '' for i in range(0, len(fileName)): if fileName[i] !='_': filenamenew += fileName[i] else: break filenamenew += '.png' os.rename(fileName, filenamenew) # 文件重新命名 os.chdir(currentpath) # 改回程序运行前的工作目录 sys.stdin.flush() # 刷新 renameall(path)

     

    Processed: 0.009, SQL: 9