capture stderr from python subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
Question: I have seen this posted so many times here; yet failed to capture intentional
errors from command. 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()
    


Answer: This works perfectly 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
    

