0

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

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.