将通用内容存放在yaml中,替换时只需要yaml文件即可。
host.yaml
host: 10.96.82.148
readYaml.py
# -*- coding:utf8 -*- import os import yaml current_path = os.path.abspath(os.path.dirname(__file__)) //获取当前地址 print(current_path + "/host.yaml") //拼接yaml文件位置 hostPath = current_path + "/host.yaml" def returnHost(): with open(hostPath, 'r') as f: temp = yaml.safe_load(f.read()) print(temp) print(temp['host']) return temp['host'] if __name__ == '__main__': returnHost()