Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How can I execute bat file in new cmd window?

In my python script:

p = Popen('a.bat')

The problem is that the output of the batch file is put to main console window in which I executed my python script... I want the output of the batch file to be shown in new console window. Any help would be appreciated. Thanks.

like image 326
David Johns Avatar asked Nov 23 '12 08:11

David Johns


People also ask

How do I run a batch file in Windows?

A batch file runs like any other executable file by double-clicking the file within Windows.


2 Answers

You can set the CREATE_NEW_CONSOLE flag. For example:

import subprocess

p = subprocess.Popen('a.bat', creationflags=subprocess.CREATE_NEW_CONSOLE)

The docs regarding shell=True are inconsistent with the implementation. If you specify shell=True, it sets CREATE_NEW_CONSOLE only if the platform is either Win9x or uses the 16-bit COMMAND.COM shell.

like image 146
Eryk Sun Avatar answered Sep 21 '22 05:09

Eryk Sun


I haven't used python before, so I can't test, but this should work

p = Popen('cmd.exe /k start a.bat')
like image 37
Bali C Avatar answered Sep 20 '22 05:09

Bali C