Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stdout to dev/null不使用python3.6中的子流程模块_Python_Python 3.x_Subprocess_Python 3.6 - Fatal编程技术网

Stdout to dev/null不使用python3.6中的子流程模块

Stdout to dev/null不使用python3.6中的子流程模块,python,python-3.x,subprocess,python-3.6,Python,Python 3.x,Subprocess,Python 3.6,在ubuntu18.04.2lts上使用python3.6.7 我试图通过python脚本调用一个shell脚本,并期望stdout为null,即我不希望控制台输出 程序片段 def command_execution(self, cmd, cwd=None): """ Execute the command cmd without console output and return the exitcode """ FNULL = open(os.devnull

ubuntu18.04.2lts上使用
python3.6.7

我试图通过python脚本调用一个shell脚本,并期望stdout为null,即我不希望控制台输出

程序片段

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
如上所述,我尝试了
FNULL
subprocess.DEVNULL
方法。但是,我仍然可以在控制台上看到输出


我有什么遗漏吗

您的
cmd
是否可能输出到stderr,而不是stdout

您可以通过执行以下操作进行测试:

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