Return values from one script to another script

前端 未结 3 1555
死守一世寂寞
死守一世寂寞 2021-01-01 06:12

I have the following script that will run each script (sequentially) in a directory:

import os

directory = []

for dirpath, dirnames, filenames in os.walk(\         


        
3条回答
  •  攒了一身酷
    2021-01-01 06:41

    You can use the locals argument of execfile(). Write the scripts like this:

    def run_script():
        ret_value = 2
        return ret_value
    
    script_ret = run_script()
    

    And in your main script:

    script_locals = dict()
    execfile("path/to/script", dict(), script_locals)
    print(script_locals["script_ret"])
    

提交回复
热议问题