?
=?iso-8859-1?q?Thomas_K=F6llmann?=
Hi, everybody!
I'm teaching myself Python, and I have no experience in programming
apart from some years of shell scripting. So, please bear with me.
These two funktions are part of an administrative script I've set
myself as a first lesson, and, as you will see, they're practically the same,
except for one variable. So I'd like to weld them together -- but I
can't find out how to.
def writeIP(ip):
""" IP schreiben """
regex = re.compile('(.*)address(.*)')
confFile = open(networkConf, 'r')
conf = confFile.readlines()
confFile.close
for line in conf:
if regex.search(line):
addressLine = line
addressLineNum = conf.index(addressLine)
address = string.split(addressLine, ' ')
address[1] = ip + "\n"
conf[addressLineNum] = string.join(address)
confFile = open(networkConf, 'w')
confFile.writelines(conf)
confFile.close
def writeMask(mask):
""" netmask schreiben """
regex = re.compile('(.*)netmask(.*)')
confFile = open(networkConf, 'r')
conf = confFile.readlines()
confFile.close
for line in conf:
if regex.search(line):
netmaskLine = line
netmaskLineNum = conf.index(netmaskLine)
netmask = string.split(netmaskLine, ' ')
netmask[1] = mask + "\n"
conf[netmaskLineNum] = string.join(netmask)
confFile = open(networkConf, 'w')
confFile.writelines(conf)
confFile.close
I feel it should be possible to use something like
def writeFile(ip,keyword):
...
but how would I construct expressions like
netmaskLineNum = conf.index(netmaskLine)
in that case (netmask being the keyword here)?
I fear this is a really silly question and I guess I've missing a very
basic concept here. But I can't figure it out alone. Any advice much
appreciated!
Mit schönem Gruß
- Thomas
I'm teaching myself Python, and I have no experience in programming
apart from some years of shell scripting. So, please bear with me.
These two funktions are part of an administrative script I've set
myself as a first lesson, and, as you will see, they're practically the same,
except for one variable. So I'd like to weld them together -- but I
can't find out how to.
def writeIP(ip):
""" IP schreiben """
regex = re.compile('(.*)address(.*)')
confFile = open(networkConf, 'r')
conf = confFile.readlines()
confFile.close
for line in conf:
if regex.search(line):
addressLine = line
addressLineNum = conf.index(addressLine)
address = string.split(addressLine, ' ')
address[1] = ip + "\n"
conf[addressLineNum] = string.join(address)
confFile = open(networkConf, 'w')
confFile.writelines(conf)
confFile.close
def writeMask(mask):
""" netmask schreiben """
regex = re.compile('(.*)netmask(.*)')
confFile = open(networkConf, 'r')
conf = confFile.readlines()
confFile.close
for line in conf:
if regex.search(line):
netmaskLine = line
netmaskLineNum = conf.index(netmaskLine)
netmask = string.split(netmaskLine, ' ')
netmask[1] = mask + "\n"
conf[netmaskLineNum] = string.join(netmask)
confFile = open(networkConf, 'w')
confFile.writelines(conf)
confFile.close
I feel it should be possible to use something like
def writeFile(ip,keyword):
...
but how would I construct expressions like
netmaskLineNum = conf.index(netmaskLine)
in that case (netmask being the keyword here)?
I fear this is a really silly question and I guess I've missing a very
basic concept here. But I can't figure it out alone. Any advice much
appreciated!
Mit schönem Gruß
- Thomas