This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess.run(capture_output=Bool) does the opposite of expected
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: deleted-user-7BsjxvvFYDdTwO8F, eryksun, veky
Priority: normal Keywords:

Created on 2021-08-29 21:12 by deleted-user-7BsjxvvFYDdTwO8F, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg400561 - (view) Author: deleted-user-7BsjxvvFYDdTwO8F (deleted-user-7BsjxvvFYDdTwO8F) Date: 2021-08-29 21:12
If you run subprocess.run(capture_output=True), it doesn't show output, but if you run subprocess.run(capture_output=False) (or if you just run subprocess.run() since False is default), it does show output. In the example in the docs, it shows this in the examples section:

```py
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
```

This clearly shows capture_output showing output if true but not if false. Test code:

```py
import subprocess
subprocess.run("dir", shell=True, capture_output=False)
subprocess.run("dir", shell=True, capture_output=False)
```

Other notes: for some reason I get an error if I don't add shell=True, so maybe that contributes? I am on Windows 10 if that matters.
msg400562 - (view) Author: Vedran Čačić (veky) * Date: 2021-08-29 22:07
I think it is exactly what "capture" means: "not allow it to escape" (to the console). Maybe you should read the documentation?
msg400564 - (view) Author: deleted-user-7BsjxvvFYDdTwO8F (deleted-user-7BsjxvvFYDdTwO8F) Date: 2021-08-29 22:14
I read the documentation, and I saw in the example that when capture_output was true, command output was printed, otherwise it isn't printed. my observations directly contradict the examples given in the docs, as stated in my original issue.
msg400565 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-08-29 22:50
The documentation states that "[i]f capture_output is true, stdout and stderr will be captured". This implies a container of some kind. So look to what subprocess.run() returns: "[w]ait for command to complete, then return a CompletedProcess instance". The `stdout` attribute of a CompletedProcess is the "[c]aptured stdout from the child process". 

For example:

    >>> p = subprocess.run("dir", shell=True, capture_output=True)
    >>> p.stdout[:18]
    b' Volume in drive C'

If the output is not captured, the child process inherits the standard output/error files of the parent process, which is typically a console or terminal.

FYI, the `dir` command is internal to the CMD shell in Windows, so it only works with shell=True. There is no "dir.exe" executable that can be executed with shell=False.
msg400583 - (view) Author: deleted-user-7BsjxvvFYDdTwO8F (deleted-user-7BsjxvvFYDdTwO8F) Date: 2021-08-30 09:16
Ok, thanks, I understand now.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89211
2021-08-30 09:16:19deleted-user-7BsjxvvFYDdTwO8Fsetstatus: open -> closed
resolution: not a bug
messages: + msg400583

stage: resolved
2021-08-29 22:50:18eryksunsetnosy: + eryksun
messages: + msg400565
2021-08-29 22:14:18deleted-user-7BsjxvvFYDdTwO8Fsetmessages: + msg400564
2021-08-29 22:07:51vekysetnosy: + veky
messages: + msg400562
2021-08-29 21:12:54deleted-user-7BsjxvvFYDdTwO8Fcreate