子进程运行,check_output,当我从批处理文件运行脚本时,Popen返回空字符串

问题描述 投票:0回答:1

我有一个运行python脚本的批处理文件,并且在python脚本中,我有一个正在运行的子流程函数。

我已经尝试过subprocess.check_outputsubprocess.runsubprocess.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,...

python python-3.x batch-file subprocess tableau-server
1个回答
1
投票

问题似乎像:

© www.soinside.com 2019 - 2024. All rights reserved.