简体   繁体   中英

Create multiple files based on user input in python

i am new to python and I've written a code which create configuration files for my application. I've created the code which works for 2 IP's but it may happen that user may input more IP's and for each increase in Ip the config file will be changed. There are authentication servers and they can be either 1 or 2 only.

I am passing input to python code by a file name "inputfile", below is how it look like:

EnterIp_list: ip_1 ip_2
authentication_server: as_1 as_2

Below is how the final configuration files are created:

configfile1:                  configfile2:
App_ip: ip_1                  App_ip: ip_2
app_number: 1                 app_number: 2
authen_server: as_1           authen_server: as_2

Below is how python3 code looks:

def createconfig(filename, app_ip, app_number, authen_server)
     with open(filename, 'w') as inf:
          inf.write("App_ip=" + app_ip + "\n")
          inf.write("app_numbber=" + app_number)
          inf.write("authen_server="+ authen_server)


with open("inputfile") as f:
     for line in f:
       if EnterIP_list in line:
          a= line.split("=")
          b = a[1].split()
       if authentiation_server in line:
          c= line.split("=")
          d=c[1].split()

createconfig(configfile1, b[0], 1, d[0])
createconfig(configfile2, b[1], 2, d[1])

Users has freedom to input as many IP's as they wish for. Can someone please suggest what need to be done to make code more generic and robust so that it will work for any number of input ip's ??? also value for app_number increases with each new ip added.

There will always be two authentication server and they go in round robin eg the third app ip will be associated to "as_1" again.

You just need to iterate over your ip list in b, be aware that your current code only works for the last line of your "inputfile". As long as there is only one line, thats ok.

with open("inputfile") as f:
     for line in f:
        a= line.split("=")
        b = a[1].split()

app_count = 1
for ip in b:
    createconfig("configfile%s" % app_count , ip, app_count)
    app_count += 1

Edit: Solution updated regarding your code change.

with open("inputfile") as f:
     for line in f:
       if EnterIP_list in line:
          ips = line.split("=")[1].split()
       if authentiation_server in line:
          auth_servers = line.split("=")[1].split()

app_count = 1
for ip, auth_server in zip(ips, auth_servers):
    createconfig("configfile%s" % app_count , ip, app_count, auth_server)
    app_count += 1

A not so great way of doing it without modifying so much of your code would be to remove the last two createconfig() calls and instead do it in a loop once you have b as follows:

with open("inputfile") as f:
 for line in f:
    a= line.split("=")
    b = a[1].split()

for app_number in b:
    createconfig("configfile{}".format(app_number), b[app_number], app_number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM