繁体   English   中英

获取批处理文件的命令行输出以写入另一个文件

[英]Getting command line output of batch file to write to another file

我有一个python文件,它调用批处理文件。 批处理文件将写入命令行窗口。 如何从python文件中的批处理文件解析该输出到命令行。

我得到的唯一输出是NONENONE

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

也许为此使用命令模块而不是子进程? 它会给你命令的结果

import commands
result = commands.getoutput('ls')
print(result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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