简体   繁体   中英

Getting command line output of batch file to write to another file

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")

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM