cmd module - python
<p>I am trying to build a python shell using cmd module. </p>

<pre><code>from cmd import Cmd
import subprocess
import commands
import os 
from subprocess import call

class Pirate(Cmd): 
    intro = 'Welcome to shell\n'
    prompt = 'platform&gt; '
    pass

if __name__ == '__main__':
    Pirate().cmdloop()
</code></pre>

<p>I am trying to build a shell using python - cmd module. I am trying to build these two functionalities. </p>

<p>Welcome to shell</p>

<p>platform> ls </p>

<p>platform> cd .. </p>

<p>like if I want to perform 
ls - list all files from that directory in my python shell 
or 
cd .. - go back to prev directory </p>

<p>Can anyone help in this? 
I tried using subprocess library.. but didn't get it working. </p>

<p>Appreciate your help ! 
Ref Doc: <a href="https://docs.python.org/3/library/cmd.html" rel="nofollow">https://docs.python.org/3/library/cmd.html</a> </p>

<p>I have a hard time trying to figure out why you would need such a thing, but here's my attempt:</p>

<pre><code>import subprocess
from cmd import Cmd

class Pirate(Cmd): 
    intro = 'Welcome to shell\n'
    prompt = 'platform&gt; '

    def default(self, line): # this method will catch all commands
        subprocess.call(line, shell=True)

if __name__ == '__main__':
    Pirate().cmdloop()
</code></pre>

<p>The main point is to use <code>default</code> method to catch all commands passed as input.</p>

python shell cmd subprocess