in_filename, outfilename = '/etc/ipf.conf', '/tmp/ipf.conf'
with nested(open(in_filename), open(outfilename, 'w')) as in_:
    with out:
        for line in in_:
            out.write(line.replace(LASTKNOWN, CURRENT))
os.rename(outfilename, in_filename)
редактировать текстовый файл с помощью Python