# How to run batch file by using python code?

**URL:** https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128
**Category:** Python Help
**Created:** [July 7, 2022, 6:19am UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128 "2022-07-07T06:19:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rasheed](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@rasheed](https://discuss.python.org/u/rasheed)
#### Post date: [July 7, 2022, 6:19am UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/1 "2022-07-07T06:19:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)
#### Post date: [July 7, 2022, 7:18am UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/2 "2022-07-07T07:18:11Z")

</div>

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:

````nohighlight
```
# Put your text content here.
```

````

---

<div class="post-metadata">

### Author: ![rasheed](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@rasheed](https://discuss.python.org/u/rasheed)
#### Post date: [July 7, 2022, 7:28am UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/3 "2022-07-07T07:28:29Z")

</div>

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  
os.system(“C:\Xilinx\SDK\2018.3\bin\xsct.bat”)

---

<div class="post-metadata">

### Author: ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)
#### Post date: [July 7, 2022, 12:26pm UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/4 "2022-07-07T12:26:27Z")

</div>

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:

`test.bat`:

```nohighlight
echo test.bat was run

```

`runner.py`:

```python
import os

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

```

Terminal:

```nohighlight
C:\Users\vaclav.brozik\tmp>python runner.py
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:

```python
# escaping:
'C:\\Xilinx\\SDK\\2018.3\\bin\\xsct.bat'
# raw string:
r'C:\Xilinx\SDK\2018.3\bin\xsct.bat'

```

---

<div class="post-metadata">

### Author: ![rasheed](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@rasheed](https://discuss.python.org/u/rasheed)
#### Post date: [July 7, 2022, 1:23pm UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/5 "2022-07-07T13:23:30Z")

</div>

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()
    self.setCentralWidget(self.mdi)

    bar = self.menuBar()

    file = bar.addMenu("File")
    file.addAction("New")
    file.addAction("Cascade")
    file.addAction("Tiled") 

    file.triggered[QAction].connect(self.WindowTrig)

    self.setWindowTitle("MDI Application")

def WindowTrig(self, p):

    if p.text() == "New":
        MDIWindow.count = MDIWindow.count + 1
        sub = QMdiSubWindow()
        sub.setWidget(QTextEdit())
        sub.setWindowTitle("Sub Window " + str(MDIWindow.count))
        self.mdi.addSubWindow(sub)
        sub.show()

    if p.text() == "Cascade":
        self.mdi.cascadeSubWindows()

    if p.text() == "Tailed":
        self.mdi.tileSubWindows()

```

app = QApplication(sys.argv)  
mdiwindow = MDIWindow()  
mdiwindow.show()  
app.exec\_()

---

<div class="post-metadata">

### Author: ![rasheed](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@rasheed](https://discuss.python.org/u/rasheed)
#### Post date: [July 7, 2022, 1:34pm UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/6 "2022-07-07T13:34:39Z")

</div>

Thank You so much for detailed explanation.

---

<div class="post-metadata">

### Author: ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)
#### Post date: [July 7, 2022, 1:39pm UTC](https://discuss.python.org/t/how-to-run-batch-file-by-using-python-code/17128/7 "2022-07-07T13:39:57Z")

</div>

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.

> [@rasheed](#):
>
> I have another question

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