passing argument from python to subprocess.popen
<p>I have this code:</p>

<pre><code>from subprocess import Popen
link="abc"
theproc = Popen([sys.executable, "p1.py",link])
</code></pre>

<p>I want to send the variable "link" to p1.py,
and p1.py will print it.</p>

<p>something like this. here is p1.py:</p>

<pre><code>print "in p1.py link is "+ link
</code></pre>

<p>How can I do that?</p>

<p>I'm assuming <code>python</code> refers to Python 2.x on your system.</p>

<p>Retrieve the command line argument in <code>p1.py</code> using <code>sys.argv</code>:</p>

<pre><code>import sys

if not len(sys.argv) &gt; 1:
    print "Expecting link argument."
else:
    print "in p1.py link is " + sys.argv[1]
</code></pre>

<p>There's a function <code>subprocess.check_output</code> that is easier to use if you only want to call a program and retrieve its output:</p>

<pre><code>from subprocess import check_output

output = check_output(["python", "p1.py", "SOME_URL"])
print "p1.py returned the following output:\n'{}'".format(output)
</code></pre>

<p>Example output:</p>

<pre><code>$ python call_p1.py
p1.py returned the following output:
'in p1.py link is SOME_URL
'
</code></pre>

python subprocess popen