现在是凌晨 3 点,听着小故障暴徒,因为我出于好奇而掉进了这个兔子洞……我想给你我的代码示例,以使用 python 的内置xattr
模块。
创建一个名为的文件xattr_example.py
并将此代码放入其中,然后运行该文件。
文件:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
"""The Following Is An Example for xattr."""
# =============================================================================
import xattr
print("{}".format(xattr.__file__))
# '/usr/local/lib/python3.7/site-packages/xattr/__init__.py'
def showww_me_the_meta(file_name):
"""Using Python's XATTR to list Key Meta Names for File."""
print("Showing Initial Names & Values.")
attrz = xattr.listxattr(file_name)
result = ("A. Info Showcased Init: {}".format(attrz))
print("{}".format(result))
return result
def update_the_meta(file_name):
"""Using Python's XATTR to Update Key Meta Names for File."""
xattr.setxattr(file_name, 'custom.comment',
'I tawt I taw a puddy tat!.'.encode('utf-8'))
xattr.setxattr(file_name, 'Music.Artist',
'I did! '
'I did taw a puddy tat!'.encode('utf-8'))
get_the_meta_values(file_name)
return
def get_the_meta_values(file_name):
"""Example."""
print("B. Listing Meta for: {}".format(file_name))
attrz = xattr.listxattr(file_name)
print("")
for i in reversed(attrz):
abc = xattr.getxattr(file_name, i)
result = ("{} : {}".format(i, abc))
print(" {}".format(result))
print("")
return
def remove_the_meta(file_name):
"""Example."""
xattr.removexattr(file_name, 'custom.comment')
xattr.removexattr(file_name, 'Music.Artist')
attrz = xattr.listxattr(file_name)
result = ("C. Info Removed Meta: {}".format(attrz))
print("{}".format(result))
return result
if __name__ == '__main__':
showww_me_the_meta('xattr_example.py')
update_the_meta('xattr_example.py')
remove_the_meta('xattr_example.py')
运行文件的结果是:
$ python3 xattr_example.py
/usr/local/lib/python3.7/site-packages/xattr/__init__.py
Showing Initial Names & Values.
A. Info Showcased Init: ()
B. Listing Meta for: xattr_example.py
custom.comment : b'I tawt I taw a puddy tat!.'
Music.Artist : b'I did! I did taw a puddy tat!'
C. Info Removed Meta: ()
最后
至于"import xattr" still worked even in python3?
请注意,可以为两个版本安装python,但路径不同。
python -V
# Python 2.7.16
which python
# /usr/local/bin/python
和
python3 -V
Python 3.7.4
which python3
/usr/local/bin/python3
如果您列出了多个版本,则在调用时必须分别使用python
或。我正在使用 Mac OSX,所以我两者都有,但是这个脚本是用.python3
python
python3
参考:
希望有帮助!有一个伟大的!