Capturing stderr from a subprocess python.Popen (command, stderr = subprocess.PIPE, stdout = subprocess.PIPE)

I have seen it many times here; but failed to fix intentional errors from the team. The best partial work I have found so far.

from Tkinter import * import os import Image, ImageTk import subprocess as sub p = sub.Popen('datdsade',stdout=sub.PIPE,stderr=sub.PIPE) output, errors = p.communicate() root = Tk() text = Text(root) text.pack() text.insert(END, output+ "Error: " + errors ) root.mainloop() 
+4
python subprocess
May 27 '09 at 6:51 a.m.
source share
2 answers

This works fine for me:

 import subprocess try: #prints results result = subprocess.check_output("echo %USERNAME%", stderr=subprocess.STDOUT, shell=True) print result #causes error result = subprocess.check_output("copy testfds", stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError, ex: print "--------error------" print ex.cmd print ex.message print ex.returncode print ex.output 
+5
Jun 11 '14 at 20:03
source share

Are you 100% sure that "datdsade" actually writes stderr? If so, perhaps it buffers its stderr or blocks it.

EDIT: I suggest running β€œdatdsade” (your program) in bash (if you have Linux, you can dl sh.exe for Windows) and see if you can capture stderr in datdsade 2> errors.txt file. Keep in mind that if you are on a Windows stderr, it will not be displayed in the DOS window . You may be more fortunate to write to a log file and read it back, or have python to store in a variable.

Alternatively, stderr = sub.STDOUT will merge your errors with stdout.

EDIT AGAIN: Ignore the above, as the link () captures all of this. I would say that the problem is that the program you select never writes to stderr, or you are not actually causing an error. This is exactly how the program was written. What is a program?

+2
May 27 '09 at 6:56 a.m.
source share



All Articles