通过python批量修改文件名称

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

posted @ 2021-06-16 11:33  后山前堂客  阅读(77)  评论(0)    收藏  举报