subprocess pipe

Chris Rebert clp2 at
Sun Nov 14 20:15:53 CET 2010

On Sun, Nov 14, 2010 at 10:50 AM, Camille Harang <mammique at> wrote:
> Hi all,
> I'm having a problem with subprocess.Popen. It seems that its unable to
> capture the pg_dump's standard inputs & outputs in a non-shell mode:
> from subprocess import Popen, PIPE
> # fire pg_dump in order to read data from the file object pgsql.stdout
> pgsql = Popen(['/usr/bin/pg_dump',
>               '--host', 'localhost',
>               '--password',
>               '--username',
>               'mammique'],
>              stdin=PIPE,
>              stderr=PIPE,
>              stdout=PIPE,
>              shell=True)
> # pg_dump prompts for password so I inject it in stdin.
> pgsql.stdin.write('MY_PASSWORD' + '\n')
> In the shell mode (shell=True) pipes works but the args (username, etc.)
> are omitted. If I turn to shell=False the arguments are passed to
> pg_dump but subprocess.Popen is no longer able to capture the standard
> inputs & outputs, the password prompt appears on the TTY instead and
> doesn't take the input written in stdin. It seems that subprocess.Popen
> has only this problem with pg_dump, other interactive command lines
> seems to be correctly handled.
> Any lead?

Quoting , emphasis mine:
On Unix, with shell=True: [...] If args is a sequence, ***the first
item*** specifies the command string, and any additional items will be
treated as additional arguments ***to the shell itself***.

So if you're using shell=True, pass a single string rather than a
tokenized list. That is to say:

command = "/usr/bin/pg_dump --host localhost --password --username mammique"
pgsql = Popen(command, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)


More information about the Python-list mailing list