I have a config file where i want to replace the line starting with IPADDR=someIP
by the value stored within my variable ipaddr
my code:
for line in fileinput.input(["/etc/sysconfig/network-scripts/ifcfg-ens192"], inplace=True):
if line.strip().startswith('IPADDR='):
line ="IPADDR="+ipaddr
sys.stdout.write(str((line)) + "\n")
It does properly set the line i want, but also deletes every other line, but it should only remove blank lines, why isn't it keeping the other existing lines?
thx
Everything is good except that you are not writing the rest of the file that does not start with 'IPADDR', just add that line and all should be good.
for line in fileinput.input(["/etc/sysconfig/network-scripts/ifcfg-ens192"], inplace=True):
if line.strip().startswith('IPADDR='):
line ="IPADDR="+ipaddr
sys.stdout.write(str((line)) + "\n")
elif len(line.strip()) > 0: # add this and below line
sys.stdout.write(line) + "\n")