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 ?

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 
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' 

Comments

0

You could do this:

import os os.system('"subject:Hello World" | "/usr/bin/xxx -C -P Support\\XXX vmail"') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.