Output from subprocess.Popen()

sjdevnull at yahoo.com sjdevnull at yahoo.com
Mon Oct 16 23:17:40 CEST 2006

Clodoaldo Pinto Neto wrote:
> Output from the shell:
> [cpn at s0 teste]$ set | grep IFS
> IFS=$' \t\n'
> Output from subprocess.Popen():
> >>> import subprocess as sub
> >>> p = sub.Popen('set | grep IFS', shell=True, stdout=sub.PIPE)
> >>> p.stdout.readlines()[1]
> "IFS=' \t\n"
> Both outputs for comparison:
> IFS=$' \t\n'
> "IFS=' \t\n"
> The subprocess.Popen() output is missing the $ and the last '
> How to get the raw shell output from subprocess.Popen()?

You are getting the raw shell output, it's just that subprocess uses
the default shell (/bin/sh) and not your personal shell.  Try running
/bin/sh and run the set|grep command.

I can't see any obvious way to ask subprocess to use a shell other than
the default.
If you need a particular shell, you could use a shell script, e.g.
set|grep IFS

and call that with shell=False
p = sub.Popen('/path/to/myscript.sh', stdout=sub.PIPE)

More information about the Python-list mailing list