Python capture subprocess output after termination
问题 I am trying to get subprocess output (on Windows) at the time the TimeoutExpired exception is raised. Any ideas? try: proc = subprocess.run(cmd,timeout=3) except subprocess.TimeoutExpired: print(???) 回答1: You need to use Popen and subprocess.PIPE in order to catch the process output when timeout expires. In particular Popen.communicate is what you need. Here is an example proc = subprocess.Popen(["ping", "192.168.1.1"], stdout=subprocess.PIPE) try: output, error = proc.communicate(timeout=2)