Text mode popen - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Text mode popen (/thread-16872.html) |
Text mode popen - adam2020 - Mar-18-2019 Hi guys, first time poster sorry if my post is in the wrong format, anyway I'm following the docs - https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate prc.communicate()returns a tuple of stdout and stderr, prc = popen(command,stdout = PIPE,stderr = PIPEso the above code creates a new process I specify that stdout will be PIPE,first off what does stdout = PIPE do and how does it relate the functionality in this case of the function/method prc.communicate()? secondly I read that this will return the output of the command in bytes but what if I want to get the string of the output and not the bytes? I read that you need to to specify a text mode attribute but how is this done? thanks RE: Text mode popen - snippsat - Mar-18-2019 (Mar-18-2019, 06:05 PM)adam2020 Wrote: secondly I read that this will return the output of the command in bytes but what if I want to get the string of the output and not the bytes?Use decode() to go from bytes to string in Python 3.Also run() has made it simpler to catch output as it has a parameter capture_output=True .import subprocess out = subprocess.run(['ping', 'google.com'], capture_output=True) print(out.stdout.decode()) #Same as decode('utf-8')
RE: Text mode popen - adam2020 - Mar-18-2019 thanks snippsat great answer :) what is the main difference between sub = subprocess.run()and process = popen(['command])(and when would you choose to use one over the other? thanks RE: Text mode popen - snippsat - Mar-18-2019 (Mar-18-2019, 06:37 PM)adam2020 Wrote: and when would you choose to use one over the other?It's explained in Using the subprocess Module. Quote:The recommended approach to invoking subprocesses is to use theSo in my example run() can handle getting output from ping command,so then i should use run() .
RE: Text mode popen - adam2020 - Mar-18-2019 thanks snippsat :) only issue I am getting is that print(out.stdout.decode()) #Same as decode('utf-8') the decode() function doesn't seem to be a function of out.stdout.decode() on my implementation, also the capture output param doesn't seem to be available on my implementation neither I'm using python3 I'm using Python 3.5.2 from what I read the capture_output flag and decode() are only added in 3.7 is there any alternative? RE: Text mode popen - snippsat - Mar-18-2019 (Mar-18-2019, 07:35 PM)adam2020 Wrote: I'm using Python 3.5.2 from what I read the capture_output flag and decode() are only added in 3.7Upgrade your Python version. (Mar-18-2019, 07:35 PM)adam2020 Wrote: is there any alternative?Sure both Popen and check_output() can be used. from subprocess import check_output # linux #out = check_output(['ping', '-c', '4', 'google.com']) out = check_output(['ping', 'google.com']) print(out.decode()) import subprocess with subprocess.Popen(['ping', 'google.com'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: out, err = p.communicate() print(out.decode()) RE: Text mode popen - adam2020 - Mar-19-2019 thanks snippsat :) so many ways :p |