Nav*_*ava 1 python bash shell subprocess ubuntu-11.04
我需要执行一个shell脚本来运行via python命令中的python程序.
我应该像这样执行我的python脚本
ubuntu@ip-10-32-157-231:~/hg_intcen/lib$ xvfb-run python webpage_scrapper.py http://www.google.ca/search?q=navaspot
Run Code Online (Sandbox Code Playgroud)
这个脚本需要在python程序中执行,因为必须将巨大的链接传递给该模块.
我已经搜索过在python中执行这个shell脚本,所以我使用了"subprocess"
主要的是当你运行这个shell命令时,它需要一些时间来返回结果.我需要python模块来执行此命令,并且它必须等待while返回结果.这是必需的.
我使用subprocess.Popen它不会像我从bash那样返回结果
import subprocess
def execute_scrapping(url):
exe_cmd = "xvfb-run python lib/webpage_scrapper.py"+" "+str(url)
print "cmd:"+exe_cmd
proc = subprocess.Popen(exe_cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
time.sleep(15)
sys.stdout.flush()
d=proc.stdout.readlines()
return d[1]
Run Code Online (Sandbox Code Playgroud)
以上内容未达到确切结果.你能建议我通过python执行bash shell命令并得到结果吗?
尝试:
proc.wait()
Run Code Online (Sandbox Code Playgroud)
而不是你的time.sleep(15)电话.
来自文档:
Popen.wait() - 等待子进程终止.设置并返回returncode属性.