这是python脚本会做的。问题是如何在函数中调用外部cmd文件?
def readCSV(x):
#csvContents is a list in the global scope that will contain lists of the
#items on each line of the specified CSV file
try:
global csvContents
file = open(csvDir + x + '.csv', 'r') #Opens the CSV file
csvContents = file.read().splitlines() #Appends each line of the CSV file to csvContents
#----------------------------------------------------------------------
#This takes each item in csvContents and splits it at "," into a list.
#The list created replaces the item in csvContents
for y in range(0,len(csvContents)):
csvContents[y] = csvContents[y].lower().split(',')
if csvContents[y][6] == 'approved':
***CALL TransferProd.cmd***
file.close()
return
except Exception as error:
log(logFile, 'An error has occurred in the readCSV function: ' + str(error))
raise
答案 0 :(得分:3)
import subprocess
p = subprocess.Popen(['TransferProd.cmd'])
您可以指定输出/错误的位置(直接指向文件或文件类对象),输入管道等。
答案 1 :(得分:1)
import os
os.system('TransferProd.cmd')
这适用于unix / windows风格,因为它将命令发送到shell。但是返回值有一些变化!检查here。
答案 2 :(得分:0)
更好的解决方案是使用:
from subprocess import Popen, PIPE
proc = Popen(cmd, shell = True, close_fds = True)
stdout, stderr = proc.communicate()