如何使用python获取文件的扩展MacOS属性? [英] How to get extended MacOS attributes of a file using python?

查看:163
本文介绍了如何使用python获取文件的扩展MacOS属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣使用python从文件中获取更多信息.

I'm interested in getting more information from a file using python.

我知道使用os.stat(例如下面的代码)会返回文件信息,但是我想从文件中获取其他属性,例如来自哪里?"

I know that using os.stat (such as below) returns information on the file, but I'd like to get other attributes from the file such as 'Where from?'

os.stat(filename)

posix.stat_result(st_mode=33184, st_ino=131691855, st_dev=16777220L, st_nlink=1, st_uid=501, st_gid=20, st_size=174241, st_atime=1445046864, st_mtime=1445045836, st_ctime=1445045836)

但是,这些不是我感兴趣的属性.我有兴趣获取以下所有属性(尤其是来自哪里")

However, these aren't the attributes I'm interested. I'm interested in getting all of the below attributes (in particular 'Where from')

如何获取来源"字段?

我尝试使用os.access,但是没有返回我想要的,并且一直在搜索 OS上的文档,所以我不确定我还能尝试什么.

I've tried using os.access, but that doesn't return what I want, and have been searching the documentation on os, so I'm not sure what else I can try.

推荐答案

我的示例:

xattr -lp com.apple.metadata:kMDItemWhereFroms ~/Projects/apimon/apimon/check/tests/resources/brus-\>lux\ dec\ 28.pdf 
com.apple.metadata:kMDItemWhereFroms:
00000000  62 70 6C 69 73 74 30 30 A2 01 02 5F 10 54 68 74  |bplist00..._.Tht|
00000010  74 70 73 3A 2F 2F 62 65 2D 69 6E 74 65 72 6E 65  |tps://be-interne|
00000020  74 2E 62 65 6E 65 2D 73 79 73 74 65 6D 2E 63 6F  |t.bene-system.co|
00000030  6D 2F 2F 68 70 2F 70 64 66 73 65 72 76 69 63 65  |m//hp/pdfservice|
00000040  3F 70 64 66 3D 4B 4C 57 56 57 4C 47 31 34 34 35  |?pdf=KLWVWLG1445|
00000050  30 31 33 33 39 30 32 33 36 42 53 31 36 39 32 30  |013390236BS1690|
00000060  31 35 50 08 0B 62 00 00 00 00 00 00 01 01 00 00  |15P..b..........|
00000070  00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 63                                |.....c|
00000086

看起来好像有一个名为xattr的程序包看起来不错..(您需要pip install xattr它)

looks like there is a package called xattr doesnt look bad.. (you need to pip install xattr it)

In [6]: import xattr
In [7]: x = xattr.xattr('/Users/buzzi/Projects/apimon/apimon/check/tests/resources/brus->lux dec 28.pdf')

In [8]: x.items()
Out[8]: 
[(u'com.apple.metadata:kMDItemWhereFroms',
  'bplist00\xa2\x01\x02_\x10Thttps://be-internet.bene-system.com//hp/pdfservice?pdf=KLWVWLG1445013390236BS169201P\x08\x0bb\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00c'),
 (u'com.apple.quarantine',
  '0001;562127d1;Google Chrome;BF24C900-46D5-4F95-9B7B-C36AA6B0ACC7')]

更多研究使我进入了biplist模块. (您需要pip install biplist它)

More research lead me to biplist module. (you need to pip install biplist it)

>>> import biplist
>>> biplist.readPlistFromString(x.get('com.apple.metadata:kMDItemWhereFroms'))
['https://be-internet.bene-system.com//hp/pdfservice?pdf=KLWVWLG1445013390236BS169201',
 '']

完成!

这篇关于如何使用python获取文件的扩展MacOS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆