python - 将抓取的图片保存到当前工作目录
问题描述
我抓取了一些图像,它询问要保存图像的文件夹的路径。但是,我希望将图像直接保存到当前工作目录。
def save():
if not config.get('images'):
_alert('No images to save')
return
if _save_method.get() == 'img':
dirname = filedialog.askdirectory(mustexist=True)
_save_images(dirname)
def _save_images(dirname):
i=1
if dirname and config.get('images'):
for img in config['images']:
img_data = requests.get(img['url']).content
filename = str(i)
i=i+1
with open(filename +'.png','wb') as f:
f.write(img_data)
_alert('Done')
_scrape_btn = ttk.Button(
_mainframe, text='Scrape!', command=save
)
_scrape_btn.grid(row=2, column=0, sticky=W, pady=5)
解决方案
您可以使用.
或获取当前工作目录
import os
os.getcwd()
你能猜出它cwd
代表什么吗:)
推荐阅读
- android - 当用户从另一个页面返回时刷新页面,使用抽屉菜单导航
- ruby-on-rails - RoR:试图绘制出复杂的模型关系
- c# - C# Winforms:模拟用户点击(开始-等待数据收集-停止)
- javascript - 在 redux 上更新多个位置的值时出现问题
- google-analytics - 是否可以在不征得同意的情况下跟踪基本分析数据?
- jquery - jQuery 进度条给出内部服务器错误
- groovy - 使用 Groovy 传递凭据以访问网络驱动器
- django - django表单渲染api,自定义渲染器示例
- mysql - 如何从节点js中的mysql回调函数返回数据
- python - 如何添加到 dicts 键并将其格式化为字符串?