Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie question about subprocess.Popen() arguments

12 views
Skip to first unread message

Alex Naumov

unread,
May 23, 2013, 3:20:15 AM5/23/13
to pytho...@python.org
Hello,

I'm trying to call new process with some parameters. The problem is that the last parameter is a "string" that has a lot of spaces and different symbols like slash and so on. I can save it in file and use name of this file as parameter, but my question is: how to make it without   additional saving?

import subprocess as sp

rc = sp.Popen(["prog", "--options", "<", msg], stdin=sp.PIPE, stdout=sp.PIPE)
stdout = rc.communicate()[0]
print stdout



Thank you,
Alex

p.s.
type(msg) => <type 'str'>

Peter Otten

unread,
May 23, 2013, 3:32:40 AM5/23/13
to pytho...@python.org
Alex Naumov wrote:

> I'm trying to call new process with some parameters. The problem is that
> the last parameter is a "string" that has a lot of spaces and different
> symbols like slash and so on. I can save it in file and use name of this
> file as parameter, but my question is: how to make it without additional
> saving?
>
> import subprocess as sp
>
> rc = sp.Popen(["prog", "--options", "<", msg], stdin=sp.PIPE,
> stdout=sp.PIPE)
> stdout = rc.communicate()[0]
> print stdout

> p.s.
> type(msg) => <type 'str'>

The < operator is a shell feature, not an argument, and msg is intended to
be send to prog's stdin. The communicate() method accepts a parameter for
that. So:

rc = sp.Popen(["prog", "--options"], stdin=sp.PIPE, stdout=sp.PIPE)
stdout = rc.communicate(msg)[0]

Alex Naumov

unread,
May 23, 2013, 3:40:48 AM5/23/13
to Peter Otten, pytho...@python.org
Thank you very much, Peter!
It works!


0 new messages