我有一个运行python脚本的批处理文件,并且在python脚本中,我有一个正在运行的子流程函数。
我已经尝试过subprocess.check_output
,subprocess.run
,subprocess.Popen
,只有在使用批处理文件运行它们时,它们都向我返回一个空字符串。
如果我手动或使用IDE运行它,我将正确获得响应。以下是subprocess.run
的代码:
response = subprocess.run(fileCommand, shell=True, cwd=pSetTableauExeDirectory, capture_output=True)
self.writeInLog(' Command Response: \t' + str(response))
响应在stdout = b''] >>
在批处理文件中运行时:
命令响应:CompletedProcess(args ='tableau refreshextract--config-file“ Z:\ XXX \ tableau_config \ SampleSuperStore.txt”',returncode = 0,stdout = b'',stderr = b'')
当手动运行或在IDE中运行时:
命令响应:CompletedProcess(args ='tableau refreshextract--config-file“ Z:\ XXX \ tableau_config \ SampleSuperStore.txt”',returncode = 0,stdout = b'数据源刷新已完成。\ r \ n0行已上传。\ r \ n',stderr = b'')
运行python程序的批处理文件。参数解析到python应用程序
SET config=SampleSuperStore.txt CALL C:\XXX\AppData\Local\Continuum\anaconda3\Scripts\activate.bat C:\XXX\AppData\Local\Continuum\anaconda3\python.exe Z:\XXX\pMainManual.py "%config%"
为什么??
-完整的python代码---
try: from pWrapper import wrapper import sys except Exception as e: print(str(e)) class main: def __init__(self): self.tableauPath = 'C:\\Program Files\\Tableau\\Tableau 2018.3\\bin\\' self.tableauCommand = 'tableau refreshextract --config-file' def runJob(self,argv): self.manual_sProcess(argv[1]) def manual_sProcess(self,tableauConfigFile): new_wrapper = wrapper() new_wrapper.tableauSetup(self.tableauPath,self.tableauCommand) if new_wrapper.tableauConfigExists(tableauConfigFile): new_wrapper.tableauCommand(tableauConfigFile) if __name__ == "__main__": new_main = main() new_main.runJob(sys.argv)
包装器类:
def tableauCommand(self,tableauConfigFile): command = self.setTableauExeDirectory + ' ' + self.refreshConfigCommand + ' "' + tableauConfigFile + '"' self.new_automateTableauExtract.runCommand(tableauConfigFile,command,self.refreshConfigCommand,self.tableauFilePath,self.setTableauExeDirectory)
自动化班级:
def runCommand(self,pConfig,pCommand,pRefreshConfigCommand,pFilePath,pSetTableauExeDirectory):
try:
fileCommand = pRefreshConfigCommand + ' "' + pFilePath + '"'
response = subprocess.run(fileCommand, shell=True, cwd=pSetTableauExeDirectory, capture_output=True)
self.writeInLog(' Command Response: \t' + str(response))
except Exception as e:
self.writeInLog('Exception in function runCommand: ' + str(e))
我有一个运行python脚本的批处理文件,在python脚本中,我有一个正在运行的子流程函数。我尝试了subprocess.check_output,subprocess.run,subprocess.Popen,...
问题似乎像: