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:
- print("a")
The content inside the b.py file:
- print("b")
The content inside the c.py file:
- print("c")
Method #1 - Using Bash Script
Create another folder named bash_script. In which, test.sh exist
Now, Let see its implementation,
- #!/bin/bash
- for py_file in (find ../python_files -name *.py)
- do
- python
- 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:
- ./test.sh
Output:
- a
- b
- c
Method #2: Using Command Prompt
Now, Let see its implementation of how to run multiple files in python_files:
- python ../python_files/a.py & python ../python_files/b.py & python ../python_files/c.py
Output:
- a
- b
- c
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.
- import os
Inside os module, there is one method named system(). We will call our run script command an argument.
- os.system('python ../python_files/a.py')
Now, Let see its implementation:
- import os
- os.system('python ../python_files/a.py')
- os.system('python ../python_files/b.py')
- os.system('python ../python_files/c.py')
Output:
- a
- b
- c
Happy Coding :)
Artigos semelhantes
- What mobile games do you play daily and what keeps you coming back. If you could improve the game, what would you add?
- Do you watch anime and if so, which anime do you watch (sorry if you answered this already I'm new to Quora)?
- Como escrever scripts python em linux?
- Quais são alguns scripts Python úteis?