Run two python files at the same time

前端 未结 4 1760
遥遥无期
遥遥无期 2021-01-13 23:25

I have tried using

#!/bin/bash
python ScriptA.py &
python ScriptB.py &

to run both scripts at the same time but it always returns

4条回答
  •  执笔经年
    2021-01-14 00:00

    You have to import os module and use the system function from it, then separate the two Python files you are running by &&.

    import os
    def song():
        user = input()
        if user == "Chance":
            os.system('python ScriptA.py && python ScriptB.py')
        else:
            print("Error")
    
    song()
    

    But I will advice you to just import the two files you want to run into the third file and just run the functions in there like normal functions.

    e.g.

    import ScriptA
    import ScriptB
    
    ScriptA.function()
    ScriptB.function()
    

    If there are no functions inside the scripts, the script runs immediately after they are imported.

提交回复
热议问题