Python Forum
python run all py files from main py file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python run all py files from main py file (/thread-38422.html)



python run all py files from main py file - mg24 - Oct-11-2022

Hi Team,

Need help , is it possible
run all below py files one by one ............

I want to call main.py file and it has to run all (add,sub,mul,div) files

python main.py   

python add.py 10 20 30
python sub.py  40 20
python mul.py   2 2 2 2
python div.py   100 50



RE: python run all py files from main py file - Gribouillis - Oct-11-2022

Try this
from pathlib import Path
import sys
this_dir = Path(__file__).parent

for args in [
    "add.py 10 20 30",
    "sub.py  40 20",
    "mul.py  2 2 2 2",
    "div.py   100 50"]:
    args = args.split()
    filename = this_dir/args[0]
    args[0] = str(filename)
    sys.argv[:] = args
    namespace = {'__name__': '__main__'}
    exec(filename.read_text(), namespace)



RE: python run all py files from main py file - mg24 - Oct-11-2022

Hi Gribouillis,

superb ! Thanks for your help.

I am passing parameter to each file, where to maintain that parameter list.

each py file has parameter, I am running via command line.

Can you explain this line to me.

exec((this_dir/name).read_text(), namespace)


Thanks
mg



Thanks
mallesh


RE: python run all py files from main py file - Gribouillis - Oct-11-2022

I updated the code to include parameters. The other way to do it is to fork subprocesses. Here the line with exec reads the python code contained in each file and executes the code in the given dictionary.


RE: python run all py files from main py file - ndc85430 - Oct-11-2022

Can't each of your modules contain functions that you can call from the main one? If not, why not?


RE: python run all py files from main py file - deanhystad - Oct-11-2022

Agree with ndc85430. You should try to write a python module in a way that other modules can import the useful parts.

Let's say your add.py program adds the command line arguments, which must be numbers, and prints the result. You could write as:
add.py
import sys

def Add(*numbers):
    """Add a list of numbers,  Retrurn total."""
    return sum(numbers)

if __name__ == "__main__":
    print(sys.argv[1:])
    print(Add(*map(float, sys.argv[1:])))
You can run this as a program, but you can also import it and use the function.

file test.py
import add

print(add.Add(1, 2, 3, 4))



RE: python run all py files from main py file - mg24 - Oct-12-2022

Hi Gribouillis,

Superb ! your code worked , Clap
you are genius ! thanks for your help, you have saved my time.

Is there any other style way. I heard about multiprocessing\Multitreading\subprocess .run.



from pathlib import Path
import sys
this_dir = Path(__file__).parent
 
for args in [
    "add.py 10 20 30",
    "sub.py  40 20",
    "mul.py  2 2 2 2",
    "div.py   100 50"]:
    args = args.split()
    filename = this_dir/args[0]
    args[0] = str(filename)
    sys.argv[:] = args
    namespace = {'__name__': '__main__'}
    exec(filename.read_text(), namespace)