From a python3 script, how to I pipe a string into a bash program?
Question: As an example, here's what I've tried:

    
    
    #!/usr/bin/env python3
    
    from subprocess import Popen
    
    message = "Lo! I am up on an ox."
    Popen('less', shell=True).communicate(input=message)
    

As the last line, I also tried:

    
    
    Popen('less', stdin=message, shell=True)
    

I can do what I want with:

    
    
    Popen('echo "%s" | less' % message, shell=True)
    

Is there a more pythonic way of doing it?

Thanks!


Answer: @hyades answer above is certainly correct, and depending on what exactly you
want might be best, but the reason your second example didn't work is because
the `stdin` value must be file-like (just like unix). The following also works
for me.

    
    
    with tempfile.TemporaryFile(mode="w") as f:
         f.write(message)
         f.seek(0)
         Popen("less", stdin=f) 
    

