1

i have a text in variable called myText, and I want to run command like this:

myText = "This is my text"    
call("echo" + myText + " | mail username -s subj")

it means that I want to echo the text in myText and pass is to mail command thru pipe. what is right way to do this ?

2
  • 1
    Does this code work? If not, what does it do? What have you researched? Commented Oct 7, 2013 at 14:31
  • 2
    Did you already do from subprocess import call? If so, all you're missing is to surround myText with quotes, like call("echo '" + myText + "' | mail username -s subj") Commented Oct 7, 2013 at 14:32

1 Answer 1

3

You should have a look to os command such as popen that allows you to create a pipe to make processes communicate between each other. Check out this page

from subprocess import Popen, PIPE
p1 = Popen(['echo', myText], stdout=PIPE)
p2 = Popen('mail', stdin=p1.stdout)

This should work.

Sign up to request clarification or add additional context in comments.

3 Comments

and how this "mail username -s subj" should be written instead of 'mail' in p2 = Popen('mail', stdin=p1.stdout) ?
This is good but needs waits on p1 and p2 to let the processes complete.
@user2234234 - it would be p2 = Popen(['mail', 'username', '-s', 'subj'], stdin=p1.stdout). This example calls mail directly, skipping an intermediate shell, so it needs to break each argument out into a list.

Your Answer

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.