The variables run by the program are written into yaml and then the contents of the file are read out

I am writing an automated script recently. Encountered a headache, especially note it down.
In the process of writing a script, there is a program that can only be called in shell mode for the time being, so that the variables in the running process cannot be obtained, similar to the following:

os.system('D:\\soft\\anaconda3\\python.exe dataCenter/get_adacard_yd.py')

Try a variety of tasteless ways, if you can't get it directly, then get it indirectly. Save the variables used in get_adacard_yd.py to the yaml file. If you need to use this variable in other places, you can read it directly from the yaml file. The code is actually very simple, as follows

def yaml_w(desired_caps):
    # 将字典写入到yaml
    curpath = f'{main_t.BASE_DIR}/commonCenter'
    yamlpath = os.path.join(curpath, "caps.yaml")
    # 写入到yaml文件
    with open(yamlpath, "w", encoding="utf-8") as f:
        yaml.dump(desired_caps, f)
def yaml_r():
    curpath = f'{main_t.BASE_DIR}/commonCenter'
    yamlpath = os.path.join(curpath, "caps.yaml")
    f = open(yamlpath, 'r', encoding='utf-8')
    cfg = f.read()
    d = yaml.load(cfg,Loader=yaml.FullLoader)  # 用load方法转字典
    return d

Guess you like

Origin blog.csdn.net/kairui_guxiaobai/article/details/106364229