python批量重命名文件夹下的文件


重复的事情交给电脑做,如果一个文件夹下有很多mp3,几万个,想随机命名叫啥名字都行?

写个python脚本跑一下即可:

import os
import random
import uuid

basePath = os.path.dirname(os.path.abspath(os.path.abspath(__file__)))

workPath = os.path.join(basePath, '')

allFiles = os.listdir(workPath)


def isMp3(suffix: str):
    #print(suffix)
    if suffix == '.mp3':
        return True
    
    return False



if __name__ == '__main__':

    counter = 0
    for i in allFiles:
        name, suffix = os.path.splitext(i)
        name = '%s.mp3'%uuid.uuid4()
        if isMp3(suffix):
            oldName = os.path.join(workPath, i)
            newName = os.path.join(workPath, name)
            os.rename(oldName, newName)
            #print(newName,oldName)

            counter+=1

    print('重命名 {counter} 成功'.format(counter=counter))

原文链接:https://blog.yongit.com/note/748472.html