简体   繁体   中英

python run multiple scripts

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:\\Users\\Jia\\Downloads\\Telegram Bot\\Scripts 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.

在此处输入图片说明

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 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.

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")

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM