12

I tried using subprocess.run as described in this answer, but it doesn't return anything for stdout or stderr:

>>> result = subprocess.run('echo foo', shell=True, check=True)
>>> print(result.stdout);
None
>>> print(result.stderr);
None

I also tried using capture_output=True but I got an exception __init__() got an unexpected keyword argument 'capture_output', even though it is described in the documentation.

sashoalm
  • 75,001
  • 122
  • 434
  • 781

1 Answers1

13

I had made a mistake, I hadn't added stdout=subprocess.PIPE:

result = subprocess.run('echo foo', shell=True, check=True, stdout=subprocess.PIPE);

Now it's working.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • You might want to set `stderr` as well. – cdarke Sep 07 '18 at 09:49
  • 10
    capture_output keyword argument for subprocess.run is only available since python 3.7. You may be using an earlier version. Anyway, capture_output is equivalent to setting both stdout and strerr to subprocess.PIPE – Yakov Dan Sep 20 '18 at 16:27