pythonshstdoutstderr

With python sh module, how to preserve combined stdout and stderr?


Using the python sh module (http://amoffat.github.io/sh/index.html), how can I get the combined stdout and stderr, just like it would be if I had run the command on the terminal?


Solution

  • Here is an example redirecting stdout and stderr from sh.ls(), to the process stdout and stderr.

    import sh
    import sys
    
    sh.ls(_out=sys.stdout, _err=sys.stderr)
    

    Stdout can also be captured into a string, per the following

    import sh
    import sys
    
    s = sh.ls()
    print( s )
    

    You can also use:

    sh.ls(_err_to_out = True)