Python 删除try和except块中的重复
我在try和except块中有重复。如何从代码中删除重复项Python 删除try和except块中的重复,python,python-3.x,Python,Python 3.x,我在try和except块中有重复。如何从代码中删除重复项 def ssh_function(): for test in xyz: try: device = ConnectHandler(ip=test, username='admin', password='admin',
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')