Replace multiple text values in a single file
Question: Python 3 - attempt at Cisco Router deployment script. I am attempting to
replace multiple text values in a input value of 'router-input.txt'.
Unfortunately, I can't figure out how to replace multiple values in a single
file. At the end of running the below code, only the last value IOSOLD is
entered.

    
    
    import fileinput
    
    HOSTNAME = input("Hostname: ")
    IOSCURRENT = input("Current IOS image: ")
    IOSOLD = input("Old IOS image: ")
    
    
    f = open("router-input.txt",'r')
    filedata = f.read()
    f.close()
    
    newdata = filedata.replace("$HOSTNAME", HOSTNAME )
    newdata = filedata.replace("$IOSCURRENT", IOSCURRENT )
    newdata = filedata.replace("$IOSOLD", IOSOLD )
    
    
    f = open('output.txt','w')
    f.write(newdata)
    f.close()
    


Answer: You keep editing `filedata` and saving it in `newdata`. Each time, you're
overwriting your previous changes. Try:

    
    
    newdata = filedata.replace("$HOSTNAME", HOSTNAME )
    newdata = newdata.replace("$IOSCURRENT", IOSCURRENT )
    newdata = newdata.replace("$IOSOLD", IOSOLD )
    

