1

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 .

1
  • 3
    As a side note, flushing is an expensive operation and unneeded here. Just write and close (or use a with clause). Your data will get where it needs to go. Commented Sep 13, 2014 at 6:45

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.