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
python shell
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.
add a comment |
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
python shell
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
16 hours ago
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
16 hours ago
add a comment |
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
python shell
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
python shell
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.
edited 16 hours ago
Matthieu Brucher
4,6821127
4,6821127
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 16 hours 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
16 hours ago
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
16 hours ago
add a comment |
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
16 hours ago
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
16 hours ago
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
16 hours ago
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
16 hours ago
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
16 hours ago
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
16 hours ago
add a comment |
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:
...
add a comment |
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
could you please reframe the code and send me ?
– Spidey
16 hours 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
16 hours 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
15 hours 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
15 hours ago
add a comment |
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:
...
add a comment |
up vote
1
down vote
Python requires that if/else is indented like this:
if ...:
...
else:
...
Your code looks like this:
if ... :
...
else:
...
add a comment |
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:
...
Python requires that if/else is indented like this:
if ...:
...
else:
...
Your code looks like this:
if ... :
...
else:
...
answered 16 hours ago
Bryan Oakley
208k21239399
208k21239399
add a comment |
add a comment |
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
could you please reframe the code and send me ?
– Spidey
16 hours 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
16 hours 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
15 hours 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
15 hours ago
add a comment |
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
could you please reframe the code and send me ?
– Spidey
16 hours 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
16 hours 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
15 hours 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
15 hours ago
add a comment |
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
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
edited 16 hours ago
answered 16 hours ago
Matthieu Brucher
4,6821127
4,6821127
could you please reframe the code and send me ?
– Spidey
16 hours 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
16 hours 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
15 hours 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
15 hours ago
add a comment |
could you please reframe the code and send me ?
– Spidey
16 hours 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
16 hours 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
15 hours 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
15 hours ago
could you please reframe the code and send me ?
– Spidey
16 hours ago
could you please reframe the code and send me ?
– Spidey
16 hours 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
16 hours 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
16 hours 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
15 hours 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
15 hours 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
15 hours 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
15 hours ago
add a comment |
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.
Spidey is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
16 hours ago
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
16 hours ago