我有这段代码:
from subprocess import Popen
link="abc"
theproc = Popen([sys.executable, "p1.py",link])
我想发送变量" link"到p1.py, 和p1.py将打印出来。
像这样的事情。这是p1.py:print "in p1.py link is "+ link
我该怎么做?
答案 0 :(得分:0)
我假设python
引用了系统上的Python 2.x.
使用p1.py
sys.argv
中检索命令行参数
import sys
if not len(sys.argv) > 1:
print "Expecting link argument."
else:
print "in p1.py link is " + sys.argv[1]
如果您只想调用程序并检索其输出,那么函数subprocess.check_output
更容易使用:
from subprocess import check_output
output = check_output(["python", "p1.py", "SOME_URL"])
print "p1.py returned the following output:\n'{}'".format(output)
示例输出:
$ python call_p1.py
p1.py returned the following output:
'in p1.py link is SOME_URL
'
答案 1 :(得分:0)
您必须解析p1.py
中的命令行参数才能将其置于变量中:
import sys
try:
link = sys.argv[1]
except IndexError:
print 'argument missing'
sys.exit(1)