[英]Why else block in the code giving syntax error even indentation is proper?
我有這樣的代碼,但是else塊給出了無效的語法。 即使我感覺縮進是正確的? 有人可以幫忙嗎?
import subprocess
def ipRouteAddDelToDCNM(addDelRoute, network, prefix, gw):
try:
ha_peer = "sed -n 's/^PEER_ETH0_IP=\\(.*\\)/\\1/p' /root/packaged-files/properties/ha-setup.properties"
peer_ip = subprocess.check_output(ha_peer, shell=True).strip()
ha_role = '/usr/local/cisco/dcm/fm/ha/ha_role.sh'
state = subprocess.check_output(cmd, shell=True)
is_native_ha = getNativeHaStatus()
if is_native_ha == "SUCCESS" and "Active" in state:
#ha_role = '/usr/local/cisco/dcm/fm/ha/ha_role.sh'
#state = subprocess.check_output(cmd, shell=True)
if "Active" in state:
cmd = "route %s -net %s/%s gw %s"%(addDelRoute, network, prefix, gw)
logDHCP(cmd)
os.popen(cmd).read()
if addDelRoute == "add":
if not os.path.exists("/etc/sysconfig/network-scripts/route-eth1"):
with open("/etc/sysconfig/network-scripts/route-eth1","w+") as fw:
routeLine = "%s/%s via %s dev eth1 \n"%(network, prefix, gw)
fw.write(routeLine)
file_transfer = "scp /etc/sysconfig/network-scripts/route-eth1 root@%s:/etc/sysconfig/network-scripts/route-eth1"%(peer_ip)
file_copy = subprocess.check_output(file_transfer, shell=True)
some_format = "/etc/sysconfig/network-scripts/ifup-routes eth1"
some = subprocess.check_output(some_format, shell=True)
else:
with open("/etc/sysconfig/network-scripts/route-eth1","a") as fw:
routeLine = "%s/%s via %s dev eth1 \n"%(network, prefix, gw)
fw.write(routeLine)
file_transfer = "scp /etc/sysconfig/network-scripts/route-eth1 root@%s:/etc/sysconfig/network-scripts/route-eth1"%(peer_ip)
file_copy = subprocess.check_output(file_transfer, shell=True)
some_format = "/etc/sysconfig/network-scripts/ifup-routes eth1"
some = subprocess.check_output(some_format, shell=True)
elif addDelRoute == "del":
with open("/etc/sysconfig/network-scripts/route-eth1","r+") as f:
lines = f.readlines()
routeLine = "%s/%s via %s dev eth1"%(network, prefix, gw)
f.seek(0)
for line in lines:
if routeLine not in line:
f.write(line)
f.truncate()
file_transfer = "scp /etc/sysconfig/network-scripts/route-eth1 root@%s:/etc/sysconfig/network-scripts/route-eth1"%(peer_ip)
file_copy = subprocess.check_output(file_transfer, shell=True)
some_format = "/etc/sysconfig/network-scripts/ifup-routes eth1"
some = subprocess.check_output(some_format, shell=True)
except:
pass
您的else
塊未正確縮進,很明顯,就像上面一樣,在相同的縮進級別上,您有一個不是if
的語句。
import subprocess
def ipRouteAddDelToDCNM(addDelRoute, network, prefix, gw):
try:
ha_peer = "sed -n 's/^PEER_ETH0_IP=\\(.*\\)/\\1/p' /root/packaged-files/properties/ha-setup.properties"
peer_ip = subprocess.check_output(ha_peer, shell=True).strip()
ha_role = '/usr/local/cisco/dcm/fm/ha/ha_role.sh'
state = subprocess.check_output(cmd, shell=True)
is_native_ha = getNativeHaStatus()
if is_native_ha == "SUCCESS" and "Active" in state:
#ha_role = '/usr/local/cisco/dcm/fm/ha/ha_role.sh'
#state = subprocess.check_output(cmd, shell=True)
if "Active" in state:
cmd = "route %s -net %s/%s gw %s"%(addDelRoute, network, prefix, gw)
logDHCP(cmd)
os.popen(cmd).read()
if addDelRoute == "add":
if not os.path.exists("/etc/sysconfig/network-scripts/route-eth1"):
with open("/etc/sysconfig/network-scripts/route-eth1","w+") as fw:
routeLine = "%s/%s via %s dev eth1 \n"%(network, prefix, gw)
fw.write(routeLine)
file_transfer = "scp /etc/sysconfig/network-scripts/route-eth1 root@%s:/etc/sysconfig/network-scripts/route-eth1"%(peer_ip)
file_copy = subprocess.check_output(file_transfer, shell=True)
some_format = "/etc/sysconfig/network-scripts/ifup-routes eth1"
some = subprocess.check_output(some_format, shell=True)
else: **<--- Should be reindented**
with open("/etc/sysconfig/network-scripts/route-eth1","a") as fw:
routeLine = "%s/%s via %s dev eth1 \n"%(network, prefix, gw)
fw.write(routeLine)
file_transfer = "scp /etc/sysconfig/network-scripts/route-eth1 root@%s:/etc/sysconfig/network-scripts/route-eth1"%(peer_ip)
file_copy = subprocess.check_output(file_transfer, shell=True)
some_format = "/etc/sysconfig/network-scripts/ifup-routes eth1"
some = subprocess.check_output(some_format, shell=True)
elif addDelRoute == "del":
with open("/etc/sysconfig/network-scripts/route-eth1","r+") as f:
lines = f.readlines()
routeLine = "%s/%s via %s dev eth1"%(network, prefix, gw)
f.seek(0)
for line in lines:
if routeLine not in line:
f.write(line)
f.truncate()
file_transfer = "scp /etc/sysconfig/network-scripts/route-eth1 root@%s:/etc/sysconfig/network-scripts/route-eth1"%(peer_ip)
file_copy = subprocess.check_output(file_transfer, shell=True)
some_format = "/etc/sysconfig/network-scripts/ifup-routes eth1"
some = subprocess.check_output(some_format, shell=True)
except:
pass
Python要求if / else像這樣縮進:
if ...:
...
else:
...
您的代碼如下所示:
if ... :
...
else:
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.