1

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.

enter image description here

3
  • Multiprocessing.....? Commented Jul 16, 2021 at 9:26
  • On Windows, google for the "start" command, which will allow you to start something in a batch file without waitling for it top finish. You should start your python.exe with the path to the script file as the first parameter. If you want to run them after each other, do not use "start", but just <your path>/python.exe <script file>in your batch file. Running every schript in a folder is a very insecure design. Try to think of something more safe. Commented Jul 16, 2021 at 9:29
  • Why not just write Python code that imports the appropriate top-level functions and calls them? Commented Jul 16, 2021 at 9:35

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

2 Comments

can you check the first line? Both commands look the same to me.
Sorry, meant to put a double & for conditional execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.