import subprocess

output= subprocess.Popen(
    ("c:\\bin\\batch.bat", "an_argument", "another_argument"),
    stdout=subprocess.PIPE).stdout

for line in output:
    # do your work here

output.close()

Note that it's preferable to start your batch file with "@echo off".

Answer from tzot on Stack Overflow
🌐
Python.org
discuss.python.org › python help
How to run batch file by using python code? - Python Help - Discussions on Python.org
July 7, 2022 - I am not getting any out put while run the batch file. If any one have idea about batch file code?
Discussions

Reading output from batch file which runs a Python script - Stack Overflow
My Python script just simulates what I would see the output as. So, going into the Python script and writing to a file wouldn't solve my problem. What I need to do is read from the batch file which produces these 3 lines every 5 seconds and log them to a .txt file 2018-07-05T00:43:27.483Z+00:00 ... @ArthurBuczynski Please edit your question to be closer to what you’re actually asking, and to have enough information to be answerable. But if I can take a wild guess: you’re probably running ... More on stackoverflow.com
🌐 stackoverflow.com
July 5, 2018
subprocess - How to read batch output in python script - Stack Overflow
I am writing a script in python that will run a batch file, and after the batch is finished would proceed with more actions. I wrote the code for executing the batch and wait for it to finish. The More on stackoverflow.com
🌐 stackoverflow.com
March 25, 2018
Run a .bat file in Windows using Python code? - Stack Overflow
This gives output from batch file to be print on the python IDLE/running console. So in batch file you can echo the result in each step to debug the issue. This is also useful in automation when there is an error happening in the batch call, to understand and locate the error easily.(put "echo ... More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - Returns of running a batch file - Stack Overflow
My goal is to run a file, return its status (0 if success) and also return its output. I am using os module to have the status of running the simulation. status= os.system("file") And I am using More on stackoverflow.com
🌐 stackoverflow.com
🌐
Codeigo
codeigo.com › home › run batch file in python
Run Batch File in Python - Codeigo
April 19, 2023 - Executing the batch file with these commands prints out “Hello World” and system time on the first line of the output console, and date on the second line. When a line is preceded by the @ symbol like in line 1, the running command, echo, in this case, is hidden, and echo off turns off echoing all commands running on the script. REM is a comment command on bash. Two packages, namely, os and subprocess, can be used to execute batch files via Python.
🌐
Data to Fish
datatofish.com › batch-file-from-python
How to Run a Batch File From Python
In this tutorial, you will run a Windows batch file using Python. We also have tutorial on how to create a batch file that runs a Python script. ... Let's say, you want to create a script that runs a batch file located on your desktop, which gets the version number of your system's Windows.
🌐
Scresat
scresat.github.io › navigation › Python › 6.+Launching+Python+Programs.html
6. Launching Python Programmes
The @ symbol tells to not put these lines in the output. The %* tells to forward any command line arguments to the python code · Let's run that batch file. We don't need Double Quotation marks(") around the location as there are no space somewhere in between. Notice we use backword slash \ in the command prompt and Run for locating the files instead of forward slash / We don't need to write that .bat in the end. Run automatically assumes that the file is batch file · And we get the output!
🌐
Medium
rsajal.medium.com › running-python-script-from-batch-file-6228bd2a473
Running Python Script from Batch File | by Sajal Rastogi | Medium
August 3, 2020 - echo on & python3 -x "%~f0" %* & goto :eof # ========================================================== # # Save File as bat file # ========================================================== # # Example Python Code --> import module x = input() print(x) y = input() Make Sure to save This file with .bat extension . Input in last line is holding the screen so that user can view output. If you are showing outputs you have to put input at last else cmd will close in an instant. Useful references for learning more about batch files — ss64.com
🌐
Stack Overflow
stackoverflow.com › questions › 51181411 › reading-output-from-batch-file-which-runs-a-python-script
Reading output from batch file which runs a Python script - Stack Overflow
July 5, 2018 - I have a batch file which runs a Python script. This script simulates data that is printed 3 lines at a time every 5 seconds. Below is the simple code I have. while True: print("Some Data")
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 49474904 › how-to-read-batch-output-in-python-script
subprocess - How to read batch output in python script - Stack Overflow
March 25, 2018 - You can use communicate(). Something like - output, error = node_proc.communicate() instead of process.wait(). The python program would wait till the batch process is done with it's execution and then return the output and return code to the ...
🌐
StackHowTo
stackhowto.com › home › windows 10 › how to run a python script from a batch file
How To Run A Python Script From A Batch File - StackHowTo
January 5, 2026 - pause: Keeps the window open after the script finishes, so you can see the output. Open Notepad. Paste the code above. Save the file with a .bat extension, e.g., run_myscript.bat. Double-click the batch file to execute the script, or run it from a terminal. Running a Python script via a batch file is a quick and effective way to automate your Python-based tasks on Windows.
🌐
Delft Stack
delftstack.com › home › howto › batch › run python file in batch script
How to Run Python File in Batch Script | Delft Stack
March 4, 2025 - Open your text editor and create a new file. Save it as run_python.bat. ... Make sure to replace your_script.py with the actual name of your Python file. The @echo off command prevents the Batch script from displaying each command as it runs, ...
🌐
Reddit
reddit.com › r/learnpython › running .py alone or with using .bat files on windows cmd. this is driving me nuts.
r/learnpython on Reddit: Running .py alone or with using .bat files on Windows cmd. This is driving me nuts.
June 17, 2018 -

I'll make this brief and to the point. I need to run my .py from cmd prompt preferably using .bat files. I would like to know how to run it without a .bat file as well. This is for "Automate the Boring Stuff in Python" Chapter 6.

What I have tried:

I am using Pycharm,virtualenvs,python3.6.

-Add shebang to the very top of .py

Ex: #! or #! python -Make the .bat with this general format

Ex: @py.exe C:\Users\User\PycharmProjects\Password Locker\trysys.py %* @pause

-save as trysys.bat

-Type this is cmd Ex: C:\Users\Valler>C:\Users\User\venv\Password Locker\Scripts\python.exe C:\Users\User\PycharmProjects\Password Locker\trysys.bat

-I get this 'C:\Users\User\venv\Password' is not recognized as an internal or external command, operable program or batch file.

-I've also tried simply running trysys.bat

Possible Problems: Perhaps the issue is that Password Locker contains a space in the directory name? Perhaps the issue is I didn't add a path environmental variable correctly or at all?

Any help would be appreciated. Thanks.


Edit: With the thanks of the users below, I figured it out. So, for future finders of this post, this is for you.

running the .py with Base interpreter even though the .py is in a virtual env

In cmd line: "C:\Users\User\folder your py is in\filename.py" Quotes are necessary because there is a space in the directory name No spaces in the directory name? C:\Users\User\folderyourpyisin\filename.py is acceptable

running the .py using the interpreter in the same virtualenv

In cmd: "C:\Users\User\virtualenvfolder\Scripts\python.exe" "C:\Users\User\folder\folder your py is in\filename.py" Basically, use this specific python.exe to run this .py file. To prove that the right interpreter is being user, you do this in the .py

import sys

print(sys.executable)	This tells you which interpreter is being called

Output: "C:\Users\User\virtualenvfolder\Scripts\python.exe" "C:\Users\User\folder\folder your py is in\filename.py" C:\Users\User\virtualenvfolder\Scripts\python.exe

What about using batch files? There's a little more work involved but it can be worth the time.

running the .py file by using .bat file (This uses base interpreter!)

  1. Make a txt file in the same directory as the .py

  2. put this in it

@py.exe C:\Users\User\possiblefolder\possiblefolder\yourscript.py %*

@pause

3. Save as filename.bat 4. Edit PATH in system variables to include the directory path of the .py. It may act like it doesn't/does work when it should/shouldn't. Restart should solve this. just a cause of windows stuff See:https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services

5. Make sure Shebang at the top of the .py

\#! python3

6. Windowskey + r brings up run. 7. type in filename and any arguments 8. should run 9. Didn't run? check .bat file again. check directory names. Did you remember to use quotes if you had spaces in the directory name? Did you add the shebang at the top of the .py file? Did you restart after adding the PATH variable. Double check ALL of that.

running the .py file by using .bat file (Using the virtual environment interpreter)

  1. Make a txt file in the same directory as the .py

  2. put this in it

@C:\Users\User\virtualenvironment\possiblefolder\Scriptsr\python.exe C:\Users\User\possiblefolder\possiblefolder\yourscript.py %*

@pause

3. Save as filename.bat 4. Edit PATH in system variables to include the directory path of the .py. It may act like it doesn't/does work when it should/shouldn't. Restart should solve this. just a cause of windows stuff See:https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services

5. Make sure Shebang at the top of the .py

\#! python3

6. Windowskey + r brings up run. 7. type in filename and any arguments 8. should run

Hope that helps. Thanks for the help from everyone in the thread.

🌐
GitHub
gist.github.com › jadient › 9849314
Run python code directly from a batch file · GitHub
To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... what if I want to run some BAT code before calling the Python line? Even if I insert a blank line at #1 and have the @echo off & python on #2 I get a SyntaxError: invalid syntax. ... python reads the entire contents of batchfile.bat and skips the first line.
🌐
Medium
medium.com › @sscswapnil › one-line-of-code-to-write-batch-file-for-python-script-modern-way-971be4b8e73
One line of code to write batch file for Python Script (Modern way). | by Swapnil Chavan | Medium
August 25, 2022 - Line 3: Third line running the Python script with default Python in the system. Line 4: Fourth line is used to see the output after running the batch file · Create empty file and paste the above code with you changes related to file path and save the file with name “Automation_batch_V1.bat”. If we change the folder path we will need the change the path in batch file as well to avoid this we will perform modern way which is independent of change file path.
🌐
Rupesh Bhandari
rupeshbhandari.com.np › posts › creating-a-batch-file-to-run-a-python-script-advanced-scenarios.md
Creating a Batch File to Run a Python Script: Advanced Scenarios | Rupesh Bhandari
February 11, 2025 - Browse and select the scheduled_task.bat batch file. Finish and your Python script will run automatically according to the schedule. When automating tasks, it’s essential to capture errors or log the output for later review.