我正在尝试保存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.')
因此,如果图像是良性的,图像将转到'良性'文件夹。 当我调用函数
时get_image_file('benign/','5436e3acbae478396759f0d5')
我得到的是:
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'
我认为问题在于写入权限。我试图使用'wb','wb +','a +',但没有任何帮助。
答案 0 :(得分:2)
~/tf_files
不是有效路径,除非你在shell中工作; ~
由bash
和co。而不是操作系统扩展到您的主目录。如果你想在Python中的路径中使用波浪号,你应该在执行open
之前通过os.path.expanduser
运行它们:
path = IMAGE_PATH+class_path+str(INCR)+'.jpg'
path = os.path.expanduser(path)
with open(path, 'wb+') as f:
# ...
pass