
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('修改完畢')