Hen*_*nry 5 python module python-3.x xattr
我编写了一个脚本,使用 module 为某些文件设置扩展属性xattr。我在 Ubuntu 的 python3 中成功测试了它,但它在我的 RasperryPi 上不起作用。
我不得不更改几十个小错误,其中大部分是xattr not knowing its methods.
例如xattr.set(...) has to be changed to xattr.setattr(...). 但我未能列出它们。所以我尝试了基础知识并得到了错误:
import xattr
xattr.list('files.py')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'xattr' has no attribute 'list'
Run Code Online (Sandbox Code Playgroud)
我的想法是 python 使用了错误的模块(对于 python 2.7 而不是 3?)。所以我尝试卸载 2.7 模块,但得到了这个:
...$ pip uninstall xattr
Traceback (most recent call last):
File "/usr/local/bin/pip", line 7, in <module>
from pip._internal import main
ImportError: No module named _internal
Run Code Online (Sandbox Code Playgroud)
但我可以成功卸载 python3 包。之后“import xattr”即使在python3中仍然有效?
现在是凌晨 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')
Run Code Online (Sandbox Code Playgroud)
$ 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: ()
Run Code Online (Sandbox Code Playgroud)
请"import xattr" still worked even in python3? 注意,两个版本都可以安装 python,但路径不同。
python -V
# Python 2.7.16
which python
# /usr/local/bin/python
Run Code Online (Sandbox Code Playgroud)
和
python3 -V
Python 3.7.4
which python3
/usr/local/bin/python3
Run Code Online (Sandbox Code Playgroud)
如果列出了多个版本,则在调用时必须分别使用python或。我正在 Mac OSX 上工作,所以我两者都有,但这个脚本是用.python3pythonpython3
参考:
希望有帮助!祝你玩得开心!
| 归档时间: |
|
| 查看次数: |
3471 次 |
| 最近记录: |