How to set the current working directory in the Python sh module?
<p>Is there a way to set the current working directory within a call to the Python <a href="http://amoffat.github.io/sh/" rel="nofollow">sh</a> module?</p>

<p>I'd like to be able to execute a command --- and only the command --- in a different directory than the one I'm currently in. Something along the lines of:</p>

<pre><code>import sh
foo = sh.ls(_current_directory="/tmp")
</code></pre>

<p>woudl be nice.</p>

<p>Use the <a href="https://amoffat.github.io/sh/special_arguments.html#special-keyword-arguments" rel="nofollow"><code>_cwd</code> parameter</a> to set the current working directory on a per-command basis:</p>

<pre><code>import sh
print(sh.ls(_cwd='/tmp'))
</code></pre>

<p>This works for any command, not just <code>sh.ls</code>.</p>

python