python - 为什么我收到属性错误 "AttributeError: ' 值的对象没有属性 'interface' ”

标签 python object attributes

回溯(最近一次调用最后一次): 文件“hello.py”,第 12 行,位于 接口(interface)=选项.接口(interface) AttributeError:“值”对象没有属性“接口(interface)”

import subprocess
import optparse

parser = optparse.OptionParser()

parser.add_option("-i", "--interface", dest=" interface ", help=" Interface to change its MAC address ")

parser.add_option("-m", "--mac", dest=" mac ", help=" new mac address ")

(options, arguments) = parser.parse_args()

interface = options.interface
mac = options.mac

print("[+] Changing mac address for " + interface + " to " + mac)

subprocess.call(["ifconfig ", interface, " down"])

subprocess.call(["ifconfig ", interface, " hw", "ether", mac])

subprocess.call(["ifconfig ", interface, " up"])

注意:-我正在使用 virtualBox 来运行这个程序。

最佳答案

add_option

dest 参数定义 Values 对象的成员。您使用的名称带有前导和尾随空格。这定义了其中包含空格的成员,无法通过经典字段访问访问。

要调试它,只需执行以下操作:

print(dir(options))

打印:

[' interface ', ' mac ', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', … other members ...]

删除空格,您将能够访问interfacemac:

parser.add_option("-i", "--interface", dest="interface", help=" Interface to change its MAC address ")
parser.add_option("-m", "--mac", dest="mac", help=" new mac address ")

关于python - 为什么我收到属性错误 "AttributeError: ' 值的对象没有属性 'interface' ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638967/

相关文章:

python - SciPy KDE 梯度

c++ - 你怎么知道当前对象是什么?

C++:堆栈不会在抛出异常时展开

python - 我可以获取类的 __init__ 方法中定义的所有属性吗?

android - python-kivy buildozer 应用程序适用于 pc 但不适用于 android

python - 通过matplotlib中的箭头连接轴中的一个点和另一个轴中的另一个点

python - 从 Tensorflow 中的自定义图像数据集创建批处理

arrays - 我可以创建具有数组属性的自定义 PowerShell 对象吗?

c# - 是否有一个属性可以避免 MSTest 中的级联单元测试失败?

java - Java 中的交互类。可以有多少互动?