如何使用Python打开并修改文件属性的一个属性?

1 投票
1 回答
6720 浏览
提问于 2025-04-16 20:50

我最近遇到了一些麻烦,想找个方法打开文件的属性窗口,进入“详细信息”标签,然后编辑某个特定值的一个属性。具体来说,我在一个文件夹里有超过200个Mp3文件,我想把它们的“编号”属性值改成一个递增的数字。基本上,我预测它的样子是这样的:

import os

count = 0
directory = #directory of folder containing x amount of files
files = os.listdir(directory)
for i in files:
    count += 1
    #some code to open up i's property window
    #some code to go to the details tab
    numberProperty = #some code to get the info of the Property's Value
    numberProperty = count

总之,任何帮助我都非常感激。期待你们的回复。注意 我是在Windows 7系统上运行。

1 个回答

3

你可以试试eyeD3,这个工具可以让你修改mp3文件的ID3属性:

 import eyeD3
 tag = eyeD3.Tag()
 tag.link("/some/file.mp3")
 print tag.getArtist()
 print tag.getAlbum()
 print tag.getTitle()
 tag.setArtist(u"Cro-Mags")
 tag.setAlbum(u"Age of Quarrel")
 tag.update()

或者你也可以使用mutagen

from mutagen.easyid3 import EasyID3
audio = EasyID3("example.mp3")
audio["title"] = u"An example"
audio.save()

还有一个选择是使用songdetails(可以在Github上找到,链接我只能发两个):

import songdetails
song = songdetails.scan("data/commit.mp3")
if song is not None:
    song.artist = "Great artist"
    song.save()

希望这些对你有帮助。祝好!

撰写回答