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

Russell Jackson rusty at rcjacksonconsulting.com
Wed Sep 16 01:58:51 CEST 2009


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, and the fact that it didn't complain about that part, but I'll
try the close():

Thanks,
Rusty



On Tue, Sep 15, 2009 at 4:24 PM, Rhodri James
<rhodri at wildebst.demon.co.uk>wrote:

> On Wed, 16 Sep 2009 00:01:17 +0100, Russell Jackson <
> rusty at rcjacksonconsulting.com> wrote:
>
>  Hi,
>> I have the following code that works fine in Python 2.x, but I can't seem
>> to
>> get it to work in Python 3 with Popen. Can you please tell me how to get
>> the
>> same functionality out of Python 3? The gist of what I doing is in the
>> setpassword function. I have tried numerous ways to get this to work, and
>> just can't figure it out, and the docs on Popen are no help whatsoever on
>> how to use the now open process. The examples completely skip over what to
>> do with the process after you open it.
>>
>
> So how did it fail?
>
> ###############################################################################
>> 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)
>>        stderr = pipe.stdin.write(password)
>>        time.sleep(1)
>>        stderr = pipe.stdin.write(password)
>>        if pipe.stdin.close != 0:
>>
>
> Did you perhaps mean "if pipe.stdin.close():" ?
> Does it help if you read stdout rather than sleeping for arbitrary periods?
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Rusty

775-636-7402 Office
775-851-1982 Fax
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090915/9e3430a1/attachment.html>


More information about the Python-list mailing list