首页 > 解决方案 > 如何将python shell中的结果保存到变量中以便可以使用

问题描述

目前我正在尝试添加一个功能来在我的代码中搜索图像 url。我找到了下面的代码,但是当我运行它时,所有数据都显示(包括我想要的 url)在 python shell 中显示/打印,但没有保存到变量中。

我怎样才能得到它,以便将 shell 中显示的 url 保存到我可以使用的变量中。

谢谢

from google_images_download import google_images_download   #importing the library

response = google_images_download.googleimagesdownload()   #class instantiation

arguments = {"keywords":"air plane","limit":1,"print_urls":True}   #creating list of arguments

paths = response.download(arguments)   #passing the arguments to the function

标签: python-3.x

解决方案


我不知道它是否会起作用,但你试过了吗

 var = subprocess.check_output('python file.py',stderr=subprocess.STDOUT,shell=True)

这从 python 程序获取 shell 输出并存储它。虽然它认为它可能只是 print() 和 repr() 它将存储。

btw file.py 是生成所需输出的文件。


推荐阅读