我对Popen.communicate()有疑问.
我有返回字符串的脚本.
然后我写了第二个脚本来获取该变量.
v = "./myscript arg1 arg2"
com = subprocess.Popen(v, shell=True).communicate()
print com
Run Code Online (Sandbox Code Playgroud)
com返回(无,无).关键是我可以在第一个脚本里面打印结果,shell打印结果也是如此.我不能只将该打印分配给变量.
当然第一个脚本返回值,而不是打印它.
来自文档:
请注意,如果要将数据发送到进程的stdin,则需要使用创建
Popen对象stdin=PIPE.同样,要获得除None结果元组之外的任何内容,您还需要提供stdout=PIPE和/或支持stderr=PIPE.
因此,创建Popen对象:
subprocess.Popen("./myscript arg1 arg2", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)