How do I run multiple python scripts sequentially?
Solution 1:
A quick solution would be to use subprocess.call()
:
import subprocess
filepaths = ["C:\\Users\\harsh\\My Drive\\Folder\\Code\\3.py",
"C:\\Users\\harsh\\My Drive\\Folder\\Code\\4.py",
"C:\\Users\\harsh\\My Drive\\Folder\\Code\\5.py",
"C:\\Users\\harsh\\My Drive\\Folder\\Code\\Helper codes\\6.py",
"C:\\Users\\harsh\\My Drive\\Folder\\Code\\Process\\7.py"
]
for filepath in filepaths:
subprocess.call(["python", filepath])
However, you can also use imports, if each script's main method is laid out as such:
def main():
...
if __name__ == '__main__':
main()
Then, your script could look like the following:
import module1
import module2
... # more imports
module1.main()
module2.main()
... # more main methods
to run the main methods sequentially. This is preferable (more concise, no need to spawn new processes), but may require some refactoring.