skip to Main Content

Hi i would like to run add1.py and add2.py simultaneously and have searched BAT file and SH file but couldnt do it myself. Anyone can help me? The folder is in the following path C:UsersJiaDownloadsTelegram BotScripts
I might also add more scripts like add3.py add4.py and the list goes on. Does anyone have simple tips that can help me run every script in this folder? Thank you!

It would be even better if the script runs one after another, example add2.py runs after add1.py finishes.

enter image description here

3

Answers


  1. Just run: python add1.py & python add2.py. If you only want the second one to run if the first executes successfully, use python add1.py && python add2.py.

    Running them at the same time would use something called concurrency, which would require some modifications to your script.

    NOTE: This will only work on Windows. On Linux or MacOS, you would use: python add1.py ; python add2.py

    You can manually add more scripts. To runn every python file in a folder, you could use: python *.py if you imported them all as modules into a new file called main.py and executed them in what ever order you like in that file.

    Login or Signup to reply.
  2. You can try this, if you are just tying to run python file:

    import os
    
    lst=[l for l in os.listdir() if l.endswith(".py")]
    for ls in lst:
       os.system(f'python {ls}')
    

    Or if the name has some pattern or it is sure then, try this:

    import os
    for i in range(1,<up to last name+1>):
        os.system(f"python add{i}.py")
    
    Login or Signup to reply.
  3. As someone else already suggested, you could make a python file which executes your N python scripts.

    Using subprocess as described here: https://stackoverflow.com/a/11230471/11962413

    import subprocess
    
    subprocess.call("./test1.py", shell=True)
    subprocess.call("./test2.py", shell=True)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search