Python模块xattr没有属性列表 - python

我编写了一个脚本,该脚本使用模块xattr在某些文件上设置扩展属性。我已经在python3中的Ubuntu中成功测试了它,但是在我的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'

我有个想法,也许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

但是我可以成功卸载python3软件包。之后,即使在python3中,“导入xattr”仍然有效?

参考方案

它的凌晨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时必须分别使用python3python。我正在Mac OSX上工作,所以两者兼有,但是此脚本是用python3编写的。

参考文献:

Manual Pages
Using the xattr functions to get extended attributes. <-找到link from SO answer.
Limitations
我只能找到usage examples。

希望有帮助!有一个很棒的!

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

无法注释掉涉及多行字符串的代码 - python

基本上,我很好奇这为什么会引发语法错误,以及如何用Python的方式来“注释掉”我未使用的代码部分,例如在调试会话期间。''' def foo(): '''does nothing''' ''' 参考方案 您可以使用三重双引号注释掉三重单引…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

查找字符串中的行数 - python

我正在创建一个python电影播放器​​/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …