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
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
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
Nov 10 at 9:50
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
Nov 10 at 9:55
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
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
edited Nov 10 at 9:37
Matthieu Brucher
10.5k21935
10.5k21935
asked Nov 10 at 9:36
Spidey
13
13
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
Nov 10 at 9:50
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
Nov 10 at 9:55
add a comment |
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
Nov 10 at 9:50
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
Nov 10 at 9:55
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
Nov 10 at 9:50
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
Nov 10 at 9:50
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
Nov 10 at 9:55
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
Nov 10 at 9:55
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
Nov 10 at 9:41
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
Nov 10 at 9:58
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
Nov 10 at 10:50
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
Nov 10 at 11:01
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
add a comment |
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
});
}
});
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
Required, but never shown
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
Required, but never shown
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 Nov 10 at 9:41
Bryan Oakley
211k21249412
211k21249412
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
Nov 10 at 9:41
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
Nov 10 at 9:58
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
Nov 10 at 10:50
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
Nov 10 at 11:01
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
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
Nov 10 at 9:41
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
Nov 10 at 9:58
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
Nov 10 at 10:50
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
Nov 10 at 11:01
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
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 Nov 10 at 9:57
answered Nov 10 at 9:38
Matthieu Brucher
10.5k21935
10.5k21935
could you please reframe the code and send me ?
– Spidey
Nov 10 at 9:41
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
Nov 10 at 9:58
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
Nov 10 at 10:50
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
Nov 10 at 11:01
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
add a comment |
could you please reframe the code and send me ?
– Spidey
Nov 10 at 9:41
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
Nov 10 at 9:58
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
Nov 10 at 10:50
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
Nov 10 at 11:01
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
could you please reframe the code and send me ?
– Spidey
Nov 10 at 9:41
could you please reframe the code and send me ?
– Spidey
Nov 10 at 9:41
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
Nov 10 at 9:58
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
Nov 10 at 9:58
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
Nov 10 at 10:50
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
Nov 10 at 10:50
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
Nov 10 at 11:01
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
Nov 10 at 11:01
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
Thanks :). yes it was the transfer_file causing the issue.
– Spidey
Nov 15 at 5:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
File "/var/lib/dcnm/dhcpd.py", line 306 else: ^ SyntaxError: invalid syntax
– Spidey
Nov 10 at 9:50
else: block need to come "if not" condition fails. can you tell me how it can be properly indented ??
– Spidey
Nov 10 at 9:55