I am struggling to understand how to pipe commands using python.
What I want to do is:
echo 'subject:Hello World' | "/usr/bin/xxx -C -P Support\\XXX vmail"
I have tried this but it just throws the error "TypeError: bufsize must be an integer"
subprocess.call("echo","subject:xxx","|","/usr/bin/xxx","-C","-P","Support\\XXX","vmail")
Can this be done with python ?
Edit
I managed to get it to work using the 2 processes, but what about if I want to pipe a python object (email message) to an external program as an object rather than converting it to a string and echoing it ?
asked Apr 9, 2014 at 6:49
crankshaft
2,7075 gold badges47 silver badges84 bronze badges
3 Answers 3
Use two processes and pipe them together.
import subprocess
with open("/tmp/out.txt", "w") as o:
p1 = subprocess.Popen(["date"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["cat"], stdin=p1.stdout, stdout=o)
This is equivalent to
$ date | cat > /tmp/out.txt
answered Apr 9, 2014 at 6:59
user1907906
Sign up to request clarification or add additional context in comments.
Comments
You can use subprocess.check_output with shell=True:
output=check_output("echo subject:Hello World | /usr/bin/xxx -C -P Support\\XXX vmail", shell=True)
here's an example:
>>> output=subprocess.check_output('echo subject:Hello World | tr [:lower:] [:upper:]', shell=True)
>>> output
'SUBJECT:HELLO WORLD\n'
answered Apr 9, 2014 at 7:00
WeaselFox
7,3989 gold badges53 silver badges77 bronze badges
Comments
You could do this:
import os
os.system('"subject:Hello World" | "/usr/bin/xxx -C -P Support\\XXX vmail"')
answered Apr 9, 2014 at 7:08
Nafiul Islam
82.9k33 gold badges145 silver badges202 bronze badges
Comments
lang-py