def thing():
os.chdir("D:\Desktop\SoundTracks")
root = "D:\Desktop\SoundTracks"
temp_track_titles = []
for f in os.listdir():
temp_track = TinyTag.get(root + "\\" + f)
temp_track_titles.append(temp_track.title)
#print(temp_track.title)
#new_name = '{}-{}{}'.format(temp_track.title,temp_track.album,f_ext)
#os.rename(f,new_name)
temp_track_titles = [''.join(c for c in s if c not in string.punctuation) for s in temp_track_titles]
#print(temp_track_titles)
for i in temp_track_titles:
for f in os.listdir():
new_name = '{}{}'.format(i,'.mp3')
os.rename(f,new_name)
temp_track_titles.remove(i)
while True:
thing()
如果造成混淆,我深表歉意。我已经环顾了几个小时,找不到解决方案。基本上,我想将temp_tracks_titles列表中的名称“映射”到该文件夹中的文件。例如,列表中的名称#3应该成为文件夹中文件#3的名称。
1。)只要脚本正在运行,while True:
循环将反复重命名文件。您是[[确定这是您想做的吗?
os.listdir()
中的每个文件一次。当前,您正在为temp_track_titles
列表中的每个名称迭代目录。3。)每次remove
一个主动迭代的对象时,您都将直接跳到下一个迭代,这会使您的预期顺序混乱。遍历集合时,请勿删除集合的对象。
遵循这些注释,您将因此希望重新格式化代码:
def thing():
root = "D:\Desktop\SoundTracks"
os.chdir(root)
for f in os.listdir():
temp_track = ''.join(c for c in TinyTag.get(root + "\\" + f) if c not in string.punctuation)
new_name = '{}.mp3'.format(temp_track)
os.rename(f, new_name)
thing()