Return values from one script to another script

前端 未结 3 1564
死守一世寂寞
死守一世寂寞 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:47

    I'm just curious if there is a way to return values so that the parent script can access the data.

    This is why you define functions and return values.

    Script 1 should include a function.

    def main():
        all the various bits of script 1 except the import 
        return x
    
    if __name__ == "__main__":
        x= main()
        print( x )
    

    Works the same as yours, but now can be used elsewhere

    Script 2 does this.

    import script1
    print script1.main()
    

    That's the way one script uses another.

提交回复
热议问题