繁体   English   中英

删除 try 和 except 块中的重复项

[英]remove duplication in try and except block

我在 try 和 except 块中有重复。 如何从我的代码中删除重复项?

def ssh_function():
for test in xyz:
    try:
        device = ConnectHandler(ip=test, 
                                username='admin', 
                                password='admin', 
                                device_type='cisco_ios')
        print(device.find_prompt())
        print(device.send_command('wr mem'))

    except Exception:
        device = ConnectHandler(ip=test, 
                                username='admin', 
                                password='admin', 
                                device_type='cisco_ios_telnet')  #### Different from try block
        print(device.find_prompt())
        print(device.send_command('wr mem'))

只需将重复的代码移出 try-except 子句:

try:
    device = ...
except:
    device = ...

print(device.find_prompt())
print(device.send_command('wr mem'))
print('#' * 80, '\n')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM