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 Merge all excel files in a folder using Python?
In this article, we will see how to combine all Excel files present in a folder into a single file. Module used: The python libraries used are: Pandas: Pandas is a python library developed for a python programming language for manipulating data and analyzing the data. It is widely used in Data Scien
3 min read
How to import a class from another file in Python ?
In this article, we will see How to import a class from another file in Python.Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of in
4 min read
How to run same function on multiple threads in Python?
In a large real-world application, the modules and functions have to go through a lot of input and output-based tasks like reading or updating databases, communication with different micro-services, and request-response with clients or peers. These tasks may take a significant amount of time to comp
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 and close a file in Python
There might arise a situation where one needs to interact with external files with Python. Python provides inbuilt functions for creating, writing, and reading files. In this article, we will be discussing how to open an external file and close the same using Python. Opening a file in Python There a
4 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 all CSV files in a folder in Pandas?
Our task is to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folder using glob() method and then reading the file by using pandas.read_csv() method and then displaying the content.ApproachImport Required Librarie
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