I am trying to save the image file got by requests.get()
def get_image_file(class_path,image_id):
r = requests.get(BASE_REQUEST_URL+'/image/'+image_id+'/download', stream=True)
print(r.url)
if r.status_code == 200:
with open(IMAGE_PATH+class_path+str(INCR)+'.jpg', 'wb+') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
print("Image saved")
else:
print('Can not save the image.')
So, if image is benign, image will go to 'benign' folder. When I call the function
get_image_file('benign/','5436e3acbae478396759f0d5')
Here what I get:
https://isic-archive.com/api/v1/image/5436e3acbae478396759f0d5/download
Traceback (most recent call last):
File "image_fetch.py", line 59, in <module>
get_image_file('benign/','5436e3acbae478396759f0d5')
File "image_fetch.py", line 34, in get_image_file
with open(IMAGE_PATH+class_path+str(INCR)+'.jpg', 'wb+') as f:
FileNotFoundError: [Errno 2] No such file or directory: '~/tf_files/benign/0.jpg'
I thought the problem is in the write permission.I tried to use 'wb','wb+','a+', but nothing helped.
~/tf_files
isn't a valid path, unless you're working in a shell; the ~
gets expanded to your home directory by bash
and co., not by the OS. If you want to have tildes in paths in Python, you should run them through os.path.expanduser
before doing the open
:
path = IMAGE_PATH+class_path+str(INCR)+'.jpg'
path = os.path.expanduser(path)
with open(path, 'wb+') as f:
# ...
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.