Why else block in the code giving syntax error even indentation is proper?









up vote
-1
down vote

favorite












I have a code like this but else block is giving invalid syntax. Even though I feel indentation is right ? Could someone please help ?



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









share|improve this question









New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
    – Spidey
    2 days ago










  • else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
    – Spidey
    2 days ago














up vote
-1
down vote

favorite












I have a code like this but else block is giving invalid syntax. Even though I feel indentation is right ? Could someone please help ?



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









share|improve this question









New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
    – Spidey
    2 days ago










  • else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
    – Spidey
    2 days ago












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a code like this but else block is giving invalid syntax. Even though I feel indentation is right ? Could someone please help ?



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









share|improve this question









New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a code like this but else block is giving invalid syntax. Even though I feel indentation is right ? Could someone please help ?



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






python shell






share|improve this question









New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Matthieu Brucher

4,9791127




4,9791127






New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Spidey

1




1




New contributor




Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Spidey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
    – Spidey
    2 days ago










  • else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
    – Spidey
    2 days ago
















  • File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
    – Spidey
    2 days ago










  • else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
    – Spidey
    2 days ago















File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
2 days ago




File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
2 days ago












else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
2 days ago




else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
2 days ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Python requires that if/else is indented like this:



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


Your code looks like this:



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





share|improve this answer



























    up vote
    1
    down vote













    Your else block is not indented properly, it's obvious, as just above, on the same indentation level, you have a statement that is not an 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





    share|improve this answer






















    • could you please reframe the code and send me ?
      – Spidey
      2 days ago










    • I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
      – Matthieu Brucher
      2 days ago










    • actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
      – Spidey
      2 days ago










    • As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
      – Matthieu Brucher
      2 days ago










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Spidey is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237672%2fwhy-else-block-in-the-code-giving-syntax-error-even-indentation-is-proper%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Python requires that if/else is indented like this:



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


    Your code looks like this:



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





    share|improve this answer
























      up vote
      1
      down vote













      Python requires that if/else is indented like this:



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


      Your code looks like this:



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





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Python requires that if/else is indented like this:



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


        Your code looks like this:



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





        share|improve this answer












        Python requires that if/else is indented like this:



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


        Your code looks like this:



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Bryan Oakley

        208k21239399




        208k21239399






















            up vote
            1
            down vote













            Your else block is not indented properly, it's obvious, as just above, on the same indentation level, you have a statement that is not an 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





            share|improve this answer






















            • could you please reframe the code and send me ?
              – Spidey
              2 days ago










            • I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
              – Matthieu Brucher
              2 days ago










            • actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
              – Spidey
              2 days ago










            • As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
              – Matthieu Brucher
              2 days ago














            up vote
            1
            down vote













            Your else block is not indented properly, it's obvious, as just above, on the same indentation level, you have a statement that is not an 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





            share|improve this answer






















            • could you please reframe the code and send me ?
              – Spidey
              2 days ago










            • I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
              – Matthieu Brucher
              2 days ago










            • actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
              – Spidey
              2 days ago










            • As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
              – Matthieu Brucher
              2 days ago












            up vote
            1
            down vote










            up vote
            1
            down vote









            Your else block is not indented properly, it's obvious, as just above, on the same indentation level, you have a statement that is not an 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





            share|improve this answer














            Your else block is not indented properly, it's obvious, as just above, on the same indentation level, you have a statement that is not an 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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            Matthieu Brucher

            4,9791127




            4,9791127











            • could you please reframe the code and send me ?
              – Spidey
              2 days ago










            • I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
              – Matthieu Brucher
              2 days ago










            • actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
              – Spidey
              2 days ago










            • As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
              – Matthieu Brucher
              2 days ago
















            • could you please reframe the code and send me ?
              – Spidey
              2 days ago










            • I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
              – Matthieu Brucher
              2 days ago










            • actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
              – Spidey
              2 days ago










            • As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
              – Matthieu Brucher
              2 days ago















            could you please reframe the code and send me ?
            – Spidey
            2 days ago




            could you please reframe the code and send me ?
            – Spidey
            2 days ago












            I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
            – Matthieu Brucher
            2 days ago




            I changed the indentation, but you have to refactor your code and make functions out of your blocks inside the if statements. I have no clue if the indentation is the one you expect.
            – Matthieu Brucher
            2 days ago












            actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
            – Spidey
            2 days ago




            actually the indentation is right "else block" should be placed for "if not block" not for "if block" but why it is showing the syntax error ??
            – Spidey
            2 days ago












            As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
            – Matthieu Brucher
            2 days ago




            As I've said, I don't know what is the logic of your algorithm, it's too long. Maybe it's file_transfer that should have more indentation, that's what you would know.
            – Matthieu Brucher
            2 days ago










            Spidey is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Spidey is a new contributor. Be nice, and check out our Code of Conduct.












            Spidey is a new contributor. Be nice, and check out our Code of Conduct.











            Spidey is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237672%2fwhy-else-block-in-the-code-giving-syntax-error-even-indentation-is-proper%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto