Python 使用xattr显示macOS文件注释

Python 使用xattr显示macOS文件注释,python,macos,python-2.7,Python,Macos,Python 2.7,如果我在macOS上的Python上使用xattr来显示文件的注释,它会显示无关数据: >>> from xattr import xattr >>> from pprint import pprint >>> pprint(xattr('tmp.pk.new')[u'com.apple.metadata:kMDItemFinderComment']) 'bplist00_\x10\x0fExample comment\x08\x00\x00

如果我在macOS上的Python上使用
xattr
来显示文件的注释,它会显示无关数据:

>>> from xattr import xattr
>>> from pprint import pprint
>>> pprint(xattr('tmp.pk.new')[u'com.apple.metadata:kMDItemFinderComment'])
'bplist00_\x10\x0fExample comment\x08\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a'
>>>

注释只是“示例注释”,因此显示的其他数据是什么?

finder注释保存在中,因此您必须对其进行解码才能访问内容。在Python 3中,您只需使用标准库模块
plistlib

>>> from plistlib import loads
>>> from xattr import xattr
>>> contents = xattr('tmp.pk.new')['com.apple.metadata:kMDItemFinderComment']
>>> loads(contents)
'Example comment'
如果您仍然使用Python 2,正如代码所示,您必须使用外部库,因为buildin
plistlib
不支持二进制格式,例如:

pip install biplist
python
>>> from biplist import readPlistFromString
>>> from xattr import xattr
>>> contents = xattr('tmp.pk.new')['com.apple.metadata:kMDItemFinderComment']
>>> readPlistFromString(contents)
'Example comment'

回答得很好。我使用Python2的原因是
pip3
给我带来了麻烦。