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.
The command I am running is a Perforce "p4 passwd username" command. It prompts for the password twice, and I have to enter it twice to reset a user's password. I would appreciate your help on getting this to work under Python 3. Rusty Working code in Python 2.x (Does not work in Python 3.) import os import string import sys import time ############################################################################### def setpassword(user): password = "passworD\n" p4passwd = os.popen( "p4 passwd %s" % user, 'w' ) time.sleep(1) p4passwd.write( password ) time.sleep(1) p4passwd.write( password ) if p4passwd.close() == 1: print "Password reset failed.\n" sys.exit( 1 ) ############################################################################### if __name__ == '__main__': if len (sys.argv) <= 1: print "Read the usage section at the top of the script for required parameters." sys.exit(1) user = sys.argv[1] setpassword(user) Attempted code in Python 3: (Doesn't work either) import os import string import sys import time import platform from subprocess import * if platform.system() == "Windows": p4="p4.exe" else: p4="/p4/1/bin/p4_1" ############################################################################### def log(msglevel="DEBUG", message=""): if msglevel == "TEST": print("Running in test mode. Command run would have been:\n", message) elif msglevel == "ERROR": print(message) sys.exit(1) elif (verbosity == "3"): print(message) elif (verbosity == "2" and msglevel == "INFO"): print(message) ############################################################################### 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: 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)) ############################################################################### if __name__ == '__main__': if len (sys.argv) <= 1: print ("Read the usage section at the top of the script for required parameters.") sys.exit(1) user = sys.argv[1] setpassword(user) -- Rusty 775-636-7402 Office 775-851-1982 Fax -- Rusty 775-636-7402 Office 775-851-1982 Fax
-- http://mail.python.org/mailman/listinfo/python-list