Python get bash double-tab completion output
<p>I am programming a Ros interface in python, and I would like to be able to select the node I want to run from a list showing all available nodes once I choose a package.</p>

<p>In other words I would like to create the list of all nodes contained in a package getting the output I would have in the terminal if I type:</p>

<pre><code>rosrun &lt;package-name&gt; \t\t
</code></pre>

<p>In terms of python code, a wrong example of what I am trying to do could be:</p>

<pre><code>from subprocess import Popen, PIPE

p = Popen (["rosrun", "&lt;package-name&gt;", "\t\t"], stdout = PIPE, stderr = PIPE)
out, err = p.communicate ()
print (out.decode ("ascii"))
print (err.decode ("ascii"))
</code></pre>

<p>But this does not work because "\t\t" is not processed in Popen like it is in the terminal.</p>

<p>Is there any way to get this working or is it impossible to emulate the double-tab completion of the terminal from inside a python script?</p>

<p>Is Popen to be used in a different way to do this or should I totally change the code using other facilities?</p>

<p>Please help me :)</p>

<p>Finally I solved this by myself, I went right throw Ros own code and found out how it generates the bash completion output.</p>

<p>My code now is like this:</p>

<pre><code>from subprocess import Popen, PIPE
package = "&lt;package&gt;"
comm = str("catkin_find --first-only --without-underlays --libexec " + package).split ()
out, err = Popen (comm, stdout = PIPE, stderr = PIPE).communicate ()
out = out.decode ("ascii")
if (out.strip () == ""):
    return
comm = "find -L " + out.strip () + " -type f -exec basename {} ';'"
out, err = Popen (comm, shell = True, stdout = PIPE, stderr = PIPE).communicate ()
out = out.decode ("ascii")
print (out.strip ())
</code></pre>

<p>I simplified Ros code, it was originally more complicated, but this version does what I need for now.</p>

<p>I hope it can be useful also for others. </p>

<p>Thanks for the advices :)</p>

python bash subprocess ros