pexpect for terminal at local computer
<p>I am developing codes user interaction using pexpect for the <em>local terminal</em> on <em>Mac</em> (not remote SSH) instead of using subprocess. But I don't know what did I do wrong with following cases to receive empty outputs:</p>

<p>1)</p>

<pre><code>child = pexpect.spawn('ls')
child.expect(pexpect.EOF)
output = child.before
print output
</code></pre>

<p>The output is empty</p>

<p>2)</p>

<pre><code>child = pexpect.spawn('ls -l')
child.expect(pexpect.EOF)
output = child.before
print output
</code></pre>

<p>It works well. The output is the list of files and folder just like we type ls -l at local terminal</p>

<p>3)</p>

<pre><code>child = pexpect.spawn('pwd')
child.expect(pexpect.EOF)
output = child.before
print output
</code></pre>

<p>The output is empty</p>

<p>The output must be existing, not empty in 3 cases right? Do you know why 'ls' and 'pwd' are empty, but 'ls -l' is not? what should I do to fix 'empty' output?</p>

<p>Best regards,
Quyen Tran</p>

<p>For running commands which does not require an interaction, spawn is not the right method. It is better to use <code>pexpect.run</code> method and get the output as return value.</p>

<p><code>pexpect.spawn</code> is more suited to spawn child process where you need to send commands and expect a response. Your code works fine on my terminal but if you aren't able to get it done on yours, use <code>run</code> method</p>

<pre><code>child = pexpect.spawn('ls')
child.expect(pexpect.EOF)
0
print child.before
codes program.c
</code></pre>

python output pexpect