I got to write a script which replaces certain things in a .txt file with a specific variables from input()
and then it should create a new .txt with exact same content except the things replaced.
I have a few conditions:
Which compares line content from original .txt with a specific strings and if it's equal, it then replaces the string with replaced one and writes this new string to a new .txt file.
Basically, I must copy\paste original file with a few touches. But when it comes to the test stage, the whole config would be rewritten with only first condition stated in the build_config()
function. And If you run my script, you would see that the LACP
condition is not working at all, it writes the content in whichever way, when it should only write it if the LACP
variable equals to "y".
What's going on? Here is my code:
def build_config():
base_config = open("MES2428B.txt", 'r')
for line in base_config:
complete_config = open(location + ".txt", 'a')
complete_config.write(line)
line.replace("location", "location"+location)
complete_config.close()
base_config.close()
if lacp == "y" or "Y" or "Н" or "н" :
base_config = open("MES2428B_lacp.txt", 'r')
for line in base_config:
complete_config = open(location + ".txt", 'a')
complete_config.write(line)
base_config.close()
complete_config.close()
print("LOCATION:")
location = input()
print("VLAN:")
vlan = input()
print("Adress:")
ip = input()
print("LACP NEEDED? y/n")
lacp = input()
print("COM SPEED:")
COMspeed = input()
print("LOCATION: " + location + "\nVLAN: " + vlan + "\nIP: " + ip)
print("IS EVERYTHING RIGHT? y/n")
while True:
check = input()
if check == "y" or "Y" or "Н" or "н":
build_config()
print("Done, check it!")
input()
break
elif check == "n" or "N" or "т" or "Т":
print("What would you like to change? /n 1 - Локация /n 2 - VLAN /n 3 - IP /n 4 - COM Скорость /n 5 - nothing")
check = input()
if check == 1:
location = input()
elif check == 2:
vlan = input()
elif check == 3:
ip = input()
elif check == 4:
COMspeed = input()
elif check == 5:
print('Then why you press it!?')
build_config()
print("Done! Check it!")
input()
break
elif True:
print("Your input is garbage, try again")
'''