Jor*_*rge 4 python macos attributes operating-system xattr
我试图循环遍历 MacOS Monterrey 目录中的所有 mp3 文件,并在每次迭代时获取文件的更多信息属性,例如标题、持续时间、作者等。我发现一篇文章说使用 xattr,但是当我使用 xattr 创建变量时它不显示文件的任何属性或属性。这是在带有 xattr 包的 Python 3.9 中
import os
import xattr
directory = os.getcwd()
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f):
print(f)
x = xattr.xattr(f)
xs = x.items()
Run Code Online (Sandbox Code Playgroud)
xattr 不读取 mp3 元数据或标签,它用于读取为特定文件存储的元数据到文件系统本身,而不是存储在文件内的元数据/标签。
为了获取您需要的数据,您需要使用一些支持读取文件ID3的库来读取mp3文件本身,例如:eyed3。
这是一个小例子:
from pathlib import Path
import eyed3
root_directory = Path(".")
for filename in root_directory.rglob("*.mp3"):
mp3data = eyed3.load(filename)
if mp3data.tag != None:
print(mp3data.tag.artist)
print(mp3data.info.time_secs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
312 次 |
| 最近记录: |