简体   繁体   English

stdout到dev / null不使用python3.6中的子进程模块

[英]Stdout to dev/null not working with subprocess module in python3.6

Using Python 3.6.7 on Ubuntu 18.04.2 LTS Ubuntu 18.04.2 LTS上使用Python 3.6.7

I am trying to invoke a shell script through python script, and expect the stdout to be null ie I do not want console output. 我试图通过python脚本调用shell脚本,并期望stdout为null,即我不想要控制台输出。

Snippet of the program 该程序的片段

def command_execution(self, cmd, cwd=None):
    """ Execute the command cmd without console output
    and return the exitcode
    """
    FNULL = open(os.devnull, 'w') # Method1
    self.log.debug("Executing command " +  cmd)
    exec_cmd = subprocess.Popen(cmd, cwd=cwd, shell=True,  stdout=subprocess.DEVNULL)
    # Method1 call exec_cmd = subprocess.Popen(cmd, cwd=cwd, shell=True,  stdout=FNULL)

    (_,_) = exec_cmd.communicate()
    exitcode = exec_cmd.returncode
    self.log.debug("Executed command {0} with exitcode {1}".format(cmd, exitcode))
    return exitcode

As mentioned above, I tried both FNULL and subprocess.DEVNULL method. 如上所述,我试图既FNULLsubprocess.DEVNULL方法。 But, I still see the output on the console. 但是,我仍然在控制台上看到输出。

Am I missing anything here? 我在这里错过了什么吗?

Is it possible that your cmd is outputting to stderr, not stdout? 您的cmd可能输出到stderr而不是stdout吗?

You can test by doing something like this: 您可以通过执行以下操作来进行测试:

exec_cmd = subprocess.Popen(cmd, cwd=cwd, shell=True,  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM