当我从批处理文件和任务计划程序运行脚本时,子进程运行、check_output、Popen 返回空字符串

Jon*_*Lam 5 python subprocess batch-file python-3.x tableau-api

我有一个运行 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))
Run Code Online (Sandbox Code Playgroud)

响应在 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%"
Run Code Online (Sandbox Code Playgroud)

这是为什么??

--完整的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)  
Run Code Online (Sandbox Code Playgroud)

包装类:

def tableauCommand(self,tableauConfigFile):    
    command = self.setTableauExeDirectory + ' ' + self.refreshConfigCommand + ' "' + tableauConfigFile + '"'
    self.new_automateTableauExtract.runCommand(tableauConfigFile,command,self.refreshConfigCommand,self.tableauFilePath,self.setTableauExeDirectory)   
Run Code Online (Sandbox Code Playgroud)

自动化类:

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))
Run Code Online (Sandbox Code Playgroud)

更新:我最初认为是 bat 文件导致了这个问题,但看起来它在手动运行批处理文件时有效,但在任务计划程序上设置时无效