0

I'm using os.popen() method to run commands on cmd and get response with .read(). When I'm encountering an ERROR, the read returns '' and the output just printed. For Example -

exit_code = popen("taskkill /IM chrome.exe /F").read()
print(exit_code)
# When Chrome closed
>>> ERROR: The process "chrome.exe" not found.
>>> ''

How can I get this ERROR in my exit_code variable?

  • 1
    Try `exit_code = os.popen("taskkill /IM Xchrome.exe /F 2>&1").read()` (I use an imaginary _imagename_). You can use `2>&1` because `os.popen` runs argument in the `cmd /C` context in Windows… – JosefZ Jul 09 '21 at 19:52
  • https://stackoverflow.com/a/10683323/447901 – lit Jul 09 '21 at 20:47

0 Answers0