Python3.9 模块 "re" 和 "check_output" 当对象类型为 None

Python3.9 with module "re" and "check_output" when object type is None

我将尝试编写一个程序来搜索模式以查找何时给出了 ifconfig 的 check_output。所以我定义了一个函数,在“接口”有一个MAC的情况下,我可以继续,而如果没有MAC地址并且对象类型是None,我不能继续 else 条件。请问我哪里错了?

def get_current_mac(interface):

    pattern = r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"
    ifconfig_result = (check_output(["ifconfig", interface])).decode('utf-8')
    mac_address_result = (re.search(pattern, ifconfig_result).group())
    if mac_address_result:
    
        print("Current MAC = " + mac_address_result)

    else:
        print ('No MAC')
    

提前致谢。

此致,

RG

您不能在 None 上呼叫 .group()。您必须在 if.

中调用它
def get_current_mac(interface):
    pattern = r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"
    ifconfig_result = check_output(["ifconfig", interface]).decode('utf-8')
    mac_address_result = re.search(pattern, ifconfig_result)
    if mac_address_result:
        print("Current MAC = " + mac_address_result.group())
    else:
        print ('No MAC')