python文件名批量重命名脚本
#encoding=utf-8import cv2import osimport shutilbase_dir = r'文件所在的文件夹路径'SourseDir1 = r'源文件1名'SourseDir2 = r'源文件2名'DestDir = r'IMG'DestSourseDir1NameDirPath = os.path.join(base_dir,DestDir)if not os.path.exists(DestSourseDir1NameDirPath):os.mkdir(DestSourseDir1NameDirPath)SourseDir1SourseNames = os.listdir(os.path.join(base_dir,SourseDir1))for SourseDir1SourseName in SourseDir1SourseNames:SourseDir1SourseNamePath=os.path.join(os.path.join(base_dir,SourseDir1), SourseDir1SourseName)DestSourseDir1Name = '文件前缀'+SourseDir1SourseName#文件1修改后的名字格式DestSourseDir1NamePath = os.path.join(DestSourseDir1NameDirPath,DestSourseDir1Name)shutil.copyfile(SourseDir1SourseNamePath,DestSourseDir1NamePath)SourseDir2SourseNames = os.listdir(os.path.join(base_dir,SourseDir2SourseDir))for SourseDir2SourseName in SourseDir2SourseNames:SourseDir2SourseNamePath=os.path.join(os.path.join(base_dir,SourseDir2SourseDir), SourseDir2SourseName)DestSourseDir2Name = '文件前缀'+SourseDir2SourseName#文件2修改后的名字格式DestSourseDir2NamePath = os.path.join(DestSourseDir1NameDirPath,DestSourseDir2Name)shutil.copyfile(SourseDir2SourseNamePath,DestSourseDir2NamePath)
使用步骤
第一步:安装代码中的相应库文件,方法:pip install XXXX
。
第二步:把代码中文件名和地址做相应替换,需要修改的格式作相应修改,即可使用。