0

I have a python file which calls a batch file. The batch file writes to the command line window. How do I parse that output to the command line from the batch file inside the python file.

The only output I get is NONE and NONE

import csv
import os
import subprocess

def callGetDimmBatchFile(goodFile, badFile, logFile, batchFileName, ipAddress, userName, passWord):
        command ='{0} -i {1} -u {2} -p {3}'.format(batchFileName, ipAddress, userName, passWord)
        print(command)
        logFile.write(command + '\n')
        logFile.flush()
        p = subprocess.Popen(command)
        output = p.communicate()
        logFile.write('{0}'.format(output) + '\n')
        logFile.flush()

goodFile = open('good.txt', 'w+')
badFile = open('bad.txt', 'w+')
logFile = open('log.txt', 'w+')
batchFileName = 'getdimm.bat'
pathToCsv = 'autorun-input.csv'
print('Path to CSV is {0}'.format(pathToCsv))
counter = 0
with open(pathToCsv) as csvFile:
    reader = csv.reader(csvFile, delimiter=',')
    for row in reader:
        ipAddress = row[0]
        userName = row[1]
        passWord = row[2]
        counter += 1
        print(counter)
        logFile.write('{0}'.format(counter) + '\n')
        logFile.flush()
        callGetDimmBatchFile(goodFile, badFile, logFile, batchFileName, ipAddress, userName, passWord)

os.system("pause")
1
  • 1
    It looks like you might want something like p = subprocess.Popen(command, stdout=logFile, stderr=logFile); rc = p.wait(); print('\nReturn Code:', rc, file=logFile, flush=True). Commented Jul 4, 2015 at 17:53

1 Answer 1

1

Maybe use the commands module rather than subprocess for this? It will give you the result of the command

import commands
result = commands.getoutput('ls')
print(result)
Sign up to request clarification or add additional context in comments.

1 Comment

I am unable to use commands since I am using Python version 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.