Python bulk file specifies string substitution

Source: Internet
Author: User

In work applications, operational automation is based on standardization.

and standardization of the work, is difficult, in the company's relevant departments of cooperation.

Then, after having standardized, the corresponding deployment script, it is better to write.

Contribute to a similar environment can be used in the east.

Of course, can write better, just time is not enough.

#!/usr/bin/python#-*-coding:utf-8-*-ImportSys,os#define a list of all files that need to be replaced by IP, and be sure to use the environment, otherwise it will not work. This script is more versatile if all environments are well-definedrepip_file_list= ["D1_file",                    "D2_file",                    "D3_file",                    "D4_file",                    "D5_file"]#IP -Integrated Dictionary of the template's IP and new environment for easy replacement and better encapsulationip_dict= {"old_c_ip":"1.1.1.1",                "new_c_ip":"2.2.2.2",                "old_f_ip":"1.1.1.3",                "new_f_ip":"2.2.2.3",                "old_k_ip":"1.1.1.4",                "new_k_ip":"2.2.2.4",                "old_o_ip":"1.1.1.5",               "new_o_ip":"2.2.2.5",                "old_s_ip":"1.1.1.6",                "new_s_ip":"2.2.2.6"}#Replace the contents of the specified file with a function that replaces multiple environment parameters in parallel, so that a script can be used, multiple generic. defRepip_func (File_path, ip_dict): F= Open (File_path,'r+') All_the_lines=f.readlines () f.seek (0) f.truncate () forLineinchAll_the_lines:line= Line.replace (ip_dict['old_c_ip'],ip_dict['new_c_ip']) line= Line.replace (ip_dict['old_t_ip'],ip_dict['new_t_ip']) line= Line.replace (ip_dict['old_s_ip'],ip_dict['new_s_ip']) line= Line.replace (ip_dict['old_f_ip'],ip_dict['new_f_ip']) line= Line.replace (ip_dict['old_k_ip'],ip_dict['new_k_ip']) F.write (line) f.close ()Print "%s has replace IP%s"%(File_path, ip_dict)defMain (): forFile_pathinchRepip_file_list:repip_func (File_path, ip_dict)if __name__=="__main__": Main ()

Python bulk file specifies string substitution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.