Casa > C > Can You Run Multiple Python Scripts At Once?

Can you run multiple python scripts at once?

Hi Friend,

There can be many ways to this task, here, we will discuss a few of them. For doing this program, we have to create some python files in one folder and give some names to that folder

Let's say the folder named python_files folder contains the following python files.

The content inside the a.py file:

  1. print("a") 

The content inside the b.py file:

  1. print("b") 

The content inside the c.py file:

  1. print("c") 

Method #1 - Using Bash Script

Create another folder named bash_script. In which, test.sh exist

Now, Let see its implementation,

  1. #!/bin/bash 
  2. for py_file in (find ../python_files -name *.py) 
  3. do 
  4. python  
  5. done 

Save this content inside a bash script file( means .sh extension ). Now, It’s time to run this file. If we are using windows, so, we have to run this file in Git Bash.

Run this command in Git Bash Terminal. We can use “./” (or any valid directory spec) before the filename:

  1. ./test.sh 

Output:

Method #2: Using Command Prompt

Now, Let see its implementation of how to run multiple files in python_files:

  1. python ../python_files/a.py & python ../python_files/b.py & python ../python_files/c.py 

Output:

Method #3: Using Python File

With the help of os module, we can execute the script that can run our python files from another folder. First, We need to import the os module.

  1. import os 

Inside os module, there is one method named system(). We will call our run script command an argument.

  1. os.system('python ../python_files/a.py') 

Now, Let see its implementation:

  1. import os  
  2. os.system('python ../python_files/a.py') 
  3. os.system('python ../python_files/b.py') 
  4. os.system('python ../python_files/c.py') 

Output:

Happy Coding :)

De Foscalina Leavens

Qual é a diferença entre as licenças GPL, AGPL e LGPL? :: How to compile a Python file