开发者

How to save an Image using URL in python/django

开发者 https://www.devze.com 2023-03-08 09:52 出处:网络
If you were to save an Image using it\'s URL how would you do it ? 开发者_如何学运维 Also how do I give the Image a unique file name while saving it.

If you were to save an Image using it's URL how would you do it ?

开发者_如何学运维

Also how do I give the Image a unique file name while saving it.

    response = urllib.urlopen(image_url)
    file_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
    f = open('/media/images/temp/'+file_name, "wb")
    f.write(response.read())
    f.close()

It throws no error nor saves the file... I'm new to this I have no clue what is going wrong : |


import urllib
import string
import random
import os

filename_charset = string.ascii_letters + string.digits
filename_length = 10
file_save_dir = '/home/user/download/'

filename = ''.join(random.choice(filename_charset)
                   for s in range(filename_length))

urllib.urlretrieve ("http://www.example.com/image.png", 
                    os.path.join(file_save_dir, filename + '.png'))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号