How to Run Multiple Python Files from a Folder in Windows

If you’re just starting with Python and want to run multiple Python scripts stored in a folder, Windows provides simple ways to automate this process. This guide will walk you through everything you need to know to execute multiple Python files efficiently.

pyhton multiple

1. Organize Your Python Files

Before running the files, ensure:

  • All the Python files you want to execute are in the same folder.
  • The files have the .py extension.
  • The scripts are independent or handle their dependencies properly (e.g., avoid conflicts with files writing to the same database or file).

For example, let’s assume all your Python files are stored in:

C:\Projects\MyPythonScripts\

2. Install Python and Set Up PATH

Ensure Python is installed and added to your system’s PATH. To verify:

  1. Open Command Prompt and type:
    python --version
    
    If Python is installed, it will show the version. If not, download Python from python.org and install it.
  2. During installation, check the box for Add Python to PATH.

3. Use a Batch File to Run All Scripts

A batch file allows you to execute multiple Python files sequentially or in parallel. Here’s how to create one:

Step 1: Open Notepad

  1. Open Notepad or any text editor.
  2. Write the following script:
    @echo off
    for %%f in (*.py) do (
    echo Running %%f
    python "%%f"
    )
    pause

Step 2: Save the Batch File

  1. Save the file as run_scripts.bat.
  2. Ensure the file is saved in the same folder as your Python scripts.

Step 3: Run the Batch File

  1. Double-click the run_scripts.bat file.
  2. Command Prompt will open, and each script in the folder will run one by one.
multiple script

4. Use Python to Run All Scripts Programmatically

If you prefer using Python itself to manage the execution of multiple scripts, you can write a driver script:

Step 1: Create a Driver Script

  1. Open your favorite code editor and write the following Python code:
    import os
    folder_path = r"C:\\Projects\\MyPythonScripts\\"
    for filename in os.listdir(folder_path):
    if filename.endswith(".py"):
    file_path = os.path.join(folder_path, filename)
    print(f"Running {filename}...")
    os.system(f"python \"{file_path}\"")

Step 2: Save and Run the Driver Script

  1. Save this script as run_all_scripts.py.
  2. Open Command Prompt, navigate to the folder containing this script, and type:
    python run_all_scripts.py

5. Additional Tips

Error Handling

To handle errors in your batch file or driver script, consider redirecting errors to a log file. For example:

In the batch file:

python "%%f" 2>> errors.log

In Python:

try:
    os.system(f"python \"{file_path}\"")
except Exception as e:
    print(f"Error running {filename}: {e}")

Run Scripts in Parallel

If your scripts are independent and can run simultaneously, you can use Python’s subprocess module:

import os
import subprocess
folder_path = r"C:\\Projects\\MyPythonScripts\\"
processes = []
for filename in os.listdir(folder_path):
    if filename.endswith(".py"):
        file_path = os.path.join(folder_path, filename)
        print(f"Starting {filename}...")
        processes.append(subprocess.Popen(["python", file_path]))
# Wait for all processes to finish
for process in processes:
    process.communicate()

Conclusion

Running multiple Python files from a folder in Windows is simple and can be done using batch files or a custom Python driver script. By following the steps outlined above, you can streamline your workflow and focus on what matters most: writing great Python code!

If you have any questions or run into issues, feel free to leave a comment below.