Execute shell command and retrieve stdout in Python
Question: In Perl, if I want to execute a shell command such as `foo`, I'll do this:

    
    
    #!/usr/bin/perl
    $stdout = `foo`
    

In Python I found this very complex solution:

    
    
    #!/usr/bin/python
    import subprocess
    p = subprocess.Popen('foo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout = p.stdout.readlines()
    retval = p.wait()
    

Is there any better solution ?

Notice that I don't want to use `call` or `os.system`. I would like to place
`stdout` on a variable


Answer: An easy way is to use [`sh`](http://amoffat.github.io/sh/) package. some
examples:

    
    
    import sh
    print(sh.ls("/"))
    
    # same thing as above
    from sh import ls
    print(ls("/"))
    

