0
我有一個調用批處理文件的python文件。批處理文件寫入命令行窗口。如何從python文件中的批處理文件解析輸出到命令行。獲取批處理文件的命令行輸出以寫入另一個文件
我得到的唯一的輸出是NONE
和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")
看起來你可能要像'P = subprocess.Popen(命令,標準輸出=日誌文件,標準錯誤=日誌文件)的結果;''RC = p.wait() ;''print('\ n返回代碼:',rc,file = logFile,flush = True)'。 – eryksun
@eryksun工作原理 –