title: How to execute shell command get the output and pwd after the command in Python
body: <p>How can I execute a shell command, can be complicated like normal command in bash command line, get the output of that command and pwd after execution?</p>
  
  <p>I used function like this:</p>
  
  <pre><code>import subprocess as sub
  
  def execv(command, path):
      p = sub.Popen(['/bin/bash', '-c', command],
                      stdout=sub.PIPE, stderr=sub.STDOUT, cwd=path)
      return p.stdout.read()[:-1]
  </code></pre>
  
  <p>And I check if user use <code>cd</code> command but that will not work when user use symlink to cd or other wierd way to change directory.</p>
  
  <p>and I need a dictionary that hold <code>{'cwd': '&lt;NEW PATH&gt;', 'result': '&lt;COMMAND OUTPUT&gt;'}</code></p>