import os path = "/Users/walker/Downloads/补充截图" fileList = os.listdir(path) #得到的fileList是无序的 fileList.sort()#按照字符串顺序对fileList重新排序 # print(fileList) index = 1 index1 = 18 for fileName in fileList: if fileName.startswith("20210616"):# 20210616 的截图从序号1开始重命名 oldName = path + os.sep + fileName newName = path + os.sep + "Page" + str(index) + ".jpg" os.rename(oldName,newName) print(oldName + ‘ ---> ‘ + newName) index += 1 if fileName.startswith("20210615"):# 20210615 的截图从序号18开始重命名 oldName = path + os.sep + fileName newName = path + os.sep + "Page" + str(index1) + ".jpg" os.rename(oldName,newName) print(oldName + ‘ ---> ‘ + newName) index1 += 1
Python 3.7.4
原文:https://www.cnblogs.com/wooluwalker/p/14888731.html