Bootstrap

python 匹配某些字段删除某行

def write_network_card_cfg(interface, operate_type):
    """
    将禁用网卡信息写入到配置文件
    :param interface:
    :param operate_type:
    :return:
    """
    try:
        if operate_type == constant_define.START_CARD:
            with open(constant_define.NETWORK_CARD_CFG, "r+") as f:
                p = re.compile(interface)
                lines = [line for line in f.readlines() if p.search(line) is None]
                f.seek(0)
                f.truncate(0)
                f.writelines(lines)
        else:
            command = "ifconfig {} down".format(interface)
            with open(constant_define.NETWORK_CARD_CFG, "w+") as f:
                f.write(command)
        return True
    except Exception:
        logger.error(traceback.format_exc())
        return False
;