Its 3am, listening to the glitch mob and since I went down this rabbit hole due to my curiosity... I wanted to give you my example of code to use the python's built xattr
module.
Create a file called xattr_example.py
and put this code into it then run the file.
File:
#!/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')
Result of running the file is:
$ 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: ()
Lastly
As far as "import xattr" still worked even in python3?
Please note that python can be installed for both versions but have different paths.
python -V
# Python 2.7.16
which python
# /usr/local/bin/python
and
python3 -V
Python 3.7.4
which python3
/usr/local/bin/python3
You must use python
or python3
respectively when calling python
if you have more than one version listed. I am working on a Mac OSX so I have both, but this script is was written in python3
.
References:
Hope that helps! Have a great one!