0

I have few python ".py" files in my local system.

For example i have ".py" stored at location: "C:\Users\jack\Desktop\MyFiles"

my filenames are "my_prog1.py", "my_prog2.py" and ""my_prog3.py"

Now i want to write 4th Python script and execute all the above python script one by one in windows from the 4th python script.

Please guide me with the solution for above problem statement.

Thanks..!!

Jack
  • 1
  • 1
  • 1
  • 1
  • Possible duplicate of [Run python program from another python program (with certain requirements)](https://stackoverflow.com/questions/11356971/run-python-program-from-another-python-program-with-certain-requirements) – emsimpson92 Jun 11 '18 at 19:54
  • https://stackoverflow.com/questions/5788891/execute-a-file-with-arguments-in-python-shell – SoisJeune Jun 11 '18 at 19:55

2 Answers2

3

One way you could do it is by using imports.

Lets say you have a python script called test.py, and you have an another called test2.py, you can import test2 into test.py and run it from there. Although they would have to be in the same directory.

In your case since the 4th script will be running the other three, it would be like this.

import my_prog1
import my_prog2
import my_prog3

The other way to do this is by using the os module

import os

os.system('python my_prog1')
os.system('python my_prog2')
os.system('python my_prog3')
Angel Jimenez
  • 64
  • 1
  • 6
1

Use the import keyword in the fourth Python file to import the files that you would want to run. It would run all the files.

import my_prog1
import my_prog2
import my_prog3

Make sure all the files, including the fourth file, is in the same directory, or else this won't work.