Adding paths to arguments in popen
<p>I want to execute a Linux command through Python. This works in the terminal:</p>

<p><code>/usr/bin/myprogram --path "/home/myuser"</code></p>

<p>I've tried this:</p>

<pre><code>path = "/home/myuser"
args = ['/usr/bin/myprogram', '--path ' + path]
proc = subprocess.Popen(args)
</code></pre>

<p>And this:</p>

<pre><code>path = "/home/myuser"
args = ['/usr/bin/myprogram', '--path "' + path + '"']
proc = subprocess.Popen(args)
</code></pre>

<p>But <code>myprogram</code> does not accept the path formatting. I know that paths behave differently when not executing as shell but I can't get it working. I've also tried single quoting the path instead of double quoting it. Bonus points for a solution that also works on Windows (with a different program path, obviously).</p>

<p>EDIT: Sorry, was writing this out from memory and used backslashes instead of forward slashes. The actual code did use the (correct) forward slashes.</p>

<p>Here's something to try:</p>

<pre><code>import subprocess
import shlex

p = subprocess.Popen(shlex.split("/usr/bin/myprogram --path /home/myuser")
</code></pre>

<p>Mind the forward slashes ("/"). From what I read, Python doesn't like backslashes ("\") even when running on Windows (I've never used it on Windows myself).</p>

python linux python-2.7