1

This is silly. Every time I try to download a group of images that have periods in the file name the images show as "invalid" after being downloaded and don't open properly on my machine. Never encountered this problem before. What gives? How do I work around this?

image_url = "http://somewebsite.com/images/icon.50.png"
urllib.urlretrieve(image_url, "icon.png")

enter image description here

user1645914
  • 371
  • 6
  • 23

1 Answers1

0

Maybe the file is large enough...

Try this

def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024): 
        if chunk: # filter out keep-alive new chunks
            f.write(chunk)
            f.flush()
return local_filename
swayamraina
  • 2,958
  • 26
  • 28