Skip to content Skip to sidebar Skip to footer

Use Python To Run Commands In Batch File

I want to use python to run commands in a batch file. The screen capture below shows the batch file and commands in Windows cmd. I tried to use python to open the batch file. impo

Solution 1:

If you want to run specific commands from the bat file, you could open the bat file as a txt file (Or use the stdout output), read it line by line and then communicate with the cmd via the winpexpect module.

import winpexpect
import subprocess
import multiprocessing
cmd = winpexpect.winspawn("cmd")
# Create read write buffers
cmd.logfile_read = read_buffer
cmd.logfile_write = write_buffer
cmd.sendline("insert whatever command line you want")

edit : If I understood you correctly, I could add the read/write buffer implemntation


Solution 2:

Do you mean something like this?

import os
os.chdir("C:/Program Files/MetroCon-3.2/RepSend/")
os.startfile("RepSendQXGA64.bat")

or this?

os.system("start /wait cmd /c RepSendQXGA64.bat")

Post a Comment for "Use Python To Run Commands In Batch File"