3

Using Python 3.6.7 on Ubuntu 18.04.2 LTS

I am trying to invoke a shell script through python script, and expect the stdout to be null i.e. I do not want console output.

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. But, I still see the output on the console.

Am I missing anything here?

malhar
  • 562
  • 1
  • 9
  • 21

1 Answers1

6

Is it possible that your cmd is outputting to stderr, not stdout?

You can test by doing something like this:

exec_cmd = subprocess.Popen(cmd, cwd=cwd, shell=True,  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
ash
  • 5,139
  • 2
  • 27
  • 39
  • Thanks! That helps a lot! And I'm surprised that the output of `ffmpeg` is to `stderr` but not `stdout`. – C.K. Feb 13 '22 at 02:09