Fwd: Converting a script to Python 3 - having trouble.

Chris Rebert clp2 at rebertia.com
Wed Sep 16 03:37:43 CEST 2009

On Tue, Sep 15, 2009 at 4:58 PM, Russell Jackson
<rusty at rcjacksonconsulting.com> wrote:
> I just get an errorlevel from the executable when I read stdout, but I can't
> tell what is going on because, of course, I can't tell what Popen is
> actually doing. I never see the prompt from the executable that I would
> expect to see when I read stdout.
> I originally had the function like this:
> def setpassword(user):
>     password = "passworD\n"
>     try:
>         cmd = ' passwd {0}'.format(user)
>         pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
> stderr=PIPE, universal_newlines=True)
>         stdout = pipe.stdout.readline()
>         stderr = pipe.stdin.write(password)
>         time.sleep(1)
>         stdout = pipe.stdout.readline()
>         stderr = pipe.stdin.write(password)
>         if pipe.stdin.close != 0:
>             log("ERROR", "Password reset failed.\n{0}{1} generated the
> following error: {2}".format(p4, cmd, stderr))
>     except OSError as err:
>         log("ERROR", "Execution failed: {0}".format(err))
> but, the script just hung when I did that. I think it was stuck on the
> readline, and never got anything from the process.
> I didn't think that the if statement was incorrect based on examples I saw
> in the docs,

I'm unable to locate any example in the subprocess docs using a
similar "if". The closest is this code snippet:

rc = pipe.close()
if  rc != None and rc % 256:
    print "There were some errors"

...but that's used as an example of code *using os.popen()* in the
context of how to translate it to use subprocess.Popen().

> and the fact that it didn't complain about that part,

Well, you're just comparing the .close method of a file object for
equality with 0, which is valid, meaningful, and well-defined (hence,
no error), but not useful or relevant in this context.

>but I'll try the close():

I agree with Rhodri that that "if" statement is definitely bunk.
Rereading the docs, I think what was intended is:

if pipe.wait() != 0:


More information about the Python-list mailing list