<div dir="ltr">Hi Hugo,<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 18, 2014 at 2:59 AM, Hugo Ledoux <span dir="ltr"><<a href="mailto:h.ledoux@tudelft.nl" target="_blank">h.ledoux@tudelft.nl</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I’m developing a plugin and I want to call an external program that<br>
takes as input a string and outputs a string. Under Mac and Linux I<br>
successfully used subprocess.Popen() but under Windows it doesn’t<br>
work. For instance, from a Python console inside QGIS under Mac/Linux:<br>
<br>
import subprocess<br>
p = subprocess.Popen(['ls', '-al'], stdout=subprocess.PIPE)<br>
print p.stdout.read()<br></blockquote><div><br></div><div>Though I have not tested whether this is better on Windows, try instead:<br><br> print p.communicate()[0]<br></div><div><br></div><div>See: <a href="http://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate">http://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate</a><br>
<br></div><div>I had a similar file handle issues on Mac, and that fixed it (though it seems like p.stdout.read() should just work).<br></div><div> <br></div><div><div>Regards,<br><br></div>Larry<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
…<br>
<br>
But under Windows if I try to catch the output (which is a requirement for me) :<br>
import subprocess<br>
p = subprocess.Popen(['dir'], shell=True, stdout=subprocess.PIPE) </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Traceback (most recent call last):<br>
File "<input>", line 1, in <module><br>
File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\subprocess.py", line<br>
703, in __init__<br>
errread, errwrite) = self._get_handles(stdin, stdout, stderr)<br>
File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\subprocess.py", line<br>
839, in _get_handles<br>
p2cread = self._make_inheritable(p2cread)<br>
File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\subprocess.py", line<br>
878, in _make_inheritable<br>
_subprocess.DUPLICATE_SAME_ACCESS)<br>
WindowsError: [Error 6] The handle is invalid<br>
<br>
What’s the best way to call a program and catching the output (a<br>
string) under Windows?<br>
<br>
Many thanks,<br>
Hugo<br>
_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote></div><br></div></div></div>