Anoop P Alias
Anoop P Alias

Reputation: 403

python file.write adding multiple lines in for loop

In the following code the output adds each line 2 times

template_file = open("../conf/"+config_template_code+".tmpl",'r')
        config_out = open("../sites-enabled/"+domain_name+".conf",'w')
        for line in template_file:
                config_out.write(line.replace('CPANELIP',cpanel_ipv4))
                config_out.flush()
                config_out.write(line.replace('DOMAINNAME',domain_list))
                config_out.flush()
        template_file.close()
        config_out.close()

If i comment out one of the config_out.write it is fine; but I want 2 in place replacements in the file .

Upvotes: 1

Views: 445

Answers (1)

schesis
schesis

Reputation: 59148

You need to do the line.replace() twice, and the config_out.write() once:

                line = line.replace('CPANELIP',cpanel_ipv4)
                line = line.replace('DOMAINNAME',domain_list)
                config_out.write(line)
                config_out.flush()

Upvotes: 3

Related Questions