在 Python 中访问 Windows 文件“标签”元数据

Xev*_*ion 5 python windows metadata file

如何在 Windows 文件属性面板中访问标签属性?有我可以使用的模块吗?大多数谷歌搜索都会产生与媒体文件、文件访问时间相关的属性,但与标签、描述等元数据属性关系不大。

exif模块能够访问比我能找到的大多数属性更多的属性,但它仍然无法读取“标签”属性。

文件属性窗口

Description->属性Tags是我想要读取和写入文件的属性。

Xev*_*ion 5

有一个完整的模块专用于我想要的:IPTCInfo3

import iptcinfo3, os, sys, random, string

# Random string gennerator
rnd = lambda length=3 : ''.join(random.choices(list(string.ascii_letters), k=length))
# Path to the file, open a IPTCInfo object
path = os.path.join(sys.path[0], 'DSC_7960.jpg')
info = iptcinfo3.IPTCInfo(path)
# Show the keywords
print(info['keywords'])
# Add a keyword and save
info['keywords'] = [rnd()]
info.save()
# Remove the weird ghost file created after saving
os.remove(path + '~')
Run Code Online (Sandbox Code Playgroud)

我不太确定幽灵文件是什么或做什么,它看起来是原始文件的精确副本,因为文件大小保持不变,但无论如何,我将其删除,因为它对于实现读/写目的完全无用我需要的元数据。

我在设置关键字时注意到一些奇怪的行为,比如有些被吞入文件中(文件大小改变了,我知道它们在那里,但 Windows 不承认这一点),并且只有在手动删除之后关键词是否突然重新出现。很奇怪。