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)