How to run batch file by using python code?

I am not getting any out put while run the batch file.
If any one have idea about batch file code?

I guess that by “batch file” you mean a Python file, right?

Please show:

  • What is in the file?
  • How you run the file?
  • Any possible output.
  • What is your operating system?

The text file content, commands executed in terminal, text output enclose between lines with triple backticks:

# Put your text content here.

That is windows batch file. This batch file has commands to run a windows application. The possible output is command line window opening with a command prompt. Operating system is windows 10.

I am using this code

import os

Please use the triple backticks or the “Preformatted text” button. Your code shows up as wrong code.

Start from something simpler. This one works on a normal Windows system with Python:


echo test.bat was run

import os

print("Going to run .bat file.")
print("Finished running .bat file.")


Going to run .bat file.

C:\Users\vaclav.brozik\tmp>echo test.bat was run
test.bat was run
Finished running .bat file.

Note that on your system there could be a different way how to run Python. It could be python3 or py or maybe you need to specify the path.

Also notice that your path to the batch file contains backslashes. Those are not normal characters in a string. But you would get an error message when you try to run a batch from a mangled path.

You have to escape the backslashes by doubling them or you can use a raw string:

# escaping:
# raw string:

In a pyqt5 GUI application i am trying to run another application (which reads data from a serial port) in a sub-window. I am getting text edit sub-window but not able to get serial data similarly. could you please help in this?

I am pasting the code here,

from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QMdiArea, QAction, QMdiSubWindow
import sys

class MDIWindow(QMainWindow):
count = 0

def __init__(self):
    super(). __init__()

    self.mdi = QMdiArea()

    bar = self.menuBar()

    file = bar.addMenu("File")


    self.setWindowTitle("MDI Application")

def WindowTrig(self, p):

    if p.text() == "New":
        MDIWindow.count = MDIWindow.count + 1
        sub = QMdiSubWindow()
        sub.setWindowTitle("Sub Window " + str(MDIWindow.count))

    if p.text() == "Cascade":

    if p.text() == "Tailed":

app = QApplication(sys.argv)
mdiwindow = MDIWindow()

Thank You so much for detailed explanation.

I am glad that I could help. Did you manage to run the batch? Please let us know.

If you crate my two example files both in the same directory and find out how to run Python on your system, then it should work. Later you can resolve how to do it with paths to other directories.

I noticed you other topic but it is completely unclear what you want to do. I will add some questions there.