1. 程式人生 > >python 建立資料夾並將圖片下載到新建的資料夾中

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)