python 创建文件夹并将图片下载到新建的文件夹中

新建文件夹:

import os

import requests

response = requests.get(url)

dirname="test"

#创建名为test的文件夹

os.mkdir(dirname)

#此处相当于文件路径 test/image.jpg,也就是在test文件夹中的image.jpg文件

filename=dirname+'/'+'image.jpg'

#打开文件夹并写入图片

with open(filename,'wb') as f:

  f.write(response.content)

猜你喜欢

转载自www.cnblogs.com/hexia7935/p/9960368.html