[Tutor] subprocess and su

Alan Gauld alan.gauld at btinternet.com
Wed Aug 22 01:04:35 CEST 2007

"Tino Dai" <tinoloc at gmail.com> wrote

> PIPE=subprocess.pipe

Not sure why you need this?

> sbp=subprocess.Popen
> (["su","-",stdin=PIPE,stdout=PIPE,close_fds=True,shell=True)

and I'm not sure what that [ is doing in there? I assume you meant
to have a closing ] after the "-"?

Have you looked at the examples in the module documents?
They cover most common usages.

> sbp.communicate() to send stuff to the stdin, and get information
> out. What do get is a prompt ask for my password.

su may be spitting the prompt out to stderr. This is quite common
for Unix programs and you might have to assign Popen.stderr to PIPE
and read that too.

Otherwise you seem to be on the right track.

Oddly I just tried some experiments and I can't get subprocess
to work with the stdout=PIPE option, it used to...

Anyone have any idea what my obvious mistake is?

import subprocess as sub
po = sub.Popen('ls -l', shell=True, stdout=sub.PIPE)

Traceback (most recent call last):
  File "<input>", line 1, in ?
  File "C:\Python24\lib\subprocess.py", line 533, in __init__
    (p2cread, p2cwrite,
  File "C:\Python24\lib\subprocess.py", line 593, in _get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\Python24\lib\subprocess.py", line 634, in _make_inheritable
TypeError: an integer is required

It works without the stdout argument.


Alan G 

More information about the Tutor mailing list