python - 在Python中访问Windows文件 "Tags"元数据

标签 python windows file metadata

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

exif 模块能够 access a lot more properties比我能找到的大多数内容都要多,但它仍然无法读取“标签”属性。

File Properties Window

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

最佳答案

有一个完整的模块专门用于我想要的: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 + '~')

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

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

关于python - 在Python中访问Windows文件 "Tags"元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57421099/

相关文章:

Java - 以编程方式检查文件名中的无效字符

python - 将最佳拟合及其方程放入绘图中

python - 使用lxml在python中解析html和js

android - 在没有网络摄像头对话框的情况下启动 Android 模拟器

javascript - 如果文件长度超过1GB,如何从客户端上传文件到服务器?

c - 如何在 C 中每 3 个字符后添加一个换行符?

python - 有没有办法知道函数中传递了多少个参数?

python 等效于 filter() 获取两个输出列表(即列表的分区)

python - Unicode解码错误: 'utf-8' codec can't decode byte 0x96 in position 35: invalid start byte

windows - Windows 窗体中的主窗体