Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么代码中的块即使缩进是正确的,也会给出语法错误?_Python_Shell - Fatal编程技术网

Python 为什么代码中的块即使缩进是正确的,也会给出语法错误?

Python 为什么代码中的块即使缩进是正确的,也会给出语法错误?,python,shell,Python,Shell,我有一个这样的代码,但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" p

我有一个这样的代码,但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的语句


Python要求if/else缩进如下:

if ...:
    ...
else:
    ...
if ... :
    ...
    else:
        ...
您的代码如下所示:

if ...:
    ...
else:
    ...
if ... :
    ...
    else:
        ...

文件/var/lib/dcnm/dhcpd.py,第306行,否则:^SyntaxError:无效syntaxelse:如果not条件失败,则需要执行块。你能告诉我如何正确缩进吗?我更改了缩进,但你必须重构代码,并在if语句中用块生成函数。我不知道缩进是否是您期望的缩进。实际上缩进是正确的,否则块应该放在if not block而不是if block中,但为什么它会显示语法错误??正如我所说,我不知道您的算法的逻辑是什么,它太长了。也许是文件传输应该有更多的缩进,这是您应该知道的。谢谢:。是的,是传输文件导致了问题。