How to run multiple Python file in a folder one after another?
Last Updated :
03 Mar, 2021
In this article, we will discuss how to run multiple python files in a folder one after another. 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.
The content inside a.py file:
print("a")
The content inside b.py file:
print("b")
The content inside c.py file:
print("c")
Method 1: Using Bash Script:
We have created another folder named two. In which, test.sh exist.

Syntax:
#!/bin/bash
for python_file_name in $(find $Folder_Path -name *.py)
do
python $python_file_name
done
For running dynamically all the python program files in a given folder <FOLDER_NAME>, we can run a bash script file for doing this task. With the help of this above script, We can run all .py extension file which is located in the given folder path. With each iteration, This program will run every python file.
Now, Let see its implementation,
#!/bin/bash
for py_file in $(find ../one -name *.py)
do
python $py_file
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:
If we want to run multiple python files from another folder using our command prompt. Then, we need to take the path of the files. As in folder One, We have created three files, and now we are in folder Two.
Simple command to run our python file within a folder:
python a.py
But here, we are in another folder so, we need to take the path of the python file like below...
python ../One/a.py
Now, Let see its implementation of how to run multiple files from another folder:
python ../One/a.py & python ../One/b.py & python ../One/c.py
Output:
a
b
c
This discussed method is not effective one, because we can not write this complex command to run just few files.
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 ../One/a.py')
Now, Let see its implementation:
Python3
import os
os.system('python ../One/a.py')
os.system('python ../One/b.py')
os.system('python ../One/c.py')
Output:
a
b
c
Video Demo:
Similar Reads
How to read multiple text files from folder in Python? Reading multiple text files from a folder in Python means accessing all the .txt files stored within a specific directory and processing their contents one by one. For example, if a folder contains three text files, each with a single line of text, you might want to read each fileâs content and proc
3 min read
How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a
3 min read
How to open multiple files using "with open" in Python? Python has various highly useful methods for working with file-like objects, like the with feature. But what if we need to open several files in this manner? You wouldn't exactly be "clean" if you had a number of nested open statements. In this article, we will demonstrate how to open multiple files
2 min read
How to read multiple data files into Pandas? In this article, we are going to see how to read multiple data files into pandas, data files are of multiple types, here are a few ways to read multiple files by using the pandas package in python. The demonstrative files can be download from here Method 1: Reading CSV files If our data files are in
3 min read
How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
2 min read
Python - Move all files from subfolders to main folder This article will discuss how to move all files from the subfolder to the main folder using Python. The approach is simple it is similar to moving files from one folder to another using Python, except here the main folder or parent folder of the subfolder is passed as the destination. Modules UsedOS
3 min read