Python 批量修改mp3【文件名】为【标题】

import os
import eyed3
import random

path = 'D:/mp3/'

files = os.listdir(path) # 获得目录中所有文件
for item in files:
    item_path = path + item
    title = str.strip(eyed3.load(item_path).tag.title) # Python获得mp3文件标题 方法
    if (len(title) > 0): # 标题有值则更名
        item_path2 = path + title + ".mp3"
        while (os.path.exists(item_path2)): # 判断新文件名是否存在,存在的话 在文件名后追加随机数字
            item_path2 = path + title + str(random.randint(1, 100)) + ".mp3"
        os.rename(item_path, item_path2) # mp3 文件重命名
        print(1, item_path2)
    else:
        print(2, item_path)

print('修改完毕')

原文地址:https://www.cnblogs.com/yanzhen/p/14014999.html