subprocess pipe

Camille Harang mammique at garbure.org
Sun Nov 14 13:50:54 EST 2010


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?

Thanks,

Camille.




More information about the Python-list mailing list