Batch file package for output to Python script

I am trying to write a python script (in windows) that runs a batch file and will output the command line output of this batch file as input. In a batch file, processes that I do not have access to are executed, and produces results based on the success of these processes. I would like to take these messages from a batch file and use them in a python script. Anyone have any ideas on how to do this?

+3
source share
3 answers
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()

Please note that it is preferable to run the batch file with " @echo off".

+8
source

python script, test.bat :

import os

fh = os.popen("test.bat")
output = fh.read()
print "This is the output of test.bat:", output
fh.close()

test.bat:

@echo off
echo "This is test.bat"
+2

Try executing subprocess.Popen (). It allows you to redirect stdout and stderr to files.

+1
source

Source: https://habr.com/ru/post/1707979/


All Articles