Search code examples
pythonoutputpopen

getting output of command in subprocess


I saw a few threads for this, but everything there didn't help me.

I'm running a subprocess to run commands on cmd via python(using 2.7)

p = subprocess.Popen(["start",  "cmd", "/k", command], shell=True)

This command works and everything, but I can't manage to capture the output of the command.

I tried check_output or specifying stdout=sp.PIPE or stdout=file, but it didn't work.

Any suggestion will be appreciated.

Thanks


Solution

  • check_output should work fine:

    from subprocess import check_output
    
    out = check_output(["echo", "Test"], shell=True)
    

    Output of command:

    >>> print out
    Test