1

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 : |

4
  • 2
    This question is quite vague. If you have a URL of an image, simply fetch it using wget (or urllib if within Python). Why don't you try doing it and then post back with problems you're facing? Commented May 25, 2011 at 7:54
  • If working on it since 4 hours doesn't qualify as trying I don't know what will. Commented May 25, 2011 at 8:01
  • I'll post what I have coded....please do help Commented May 25, 2011 at 8:02
  • Guys! finally a new error - [Errno ftp error] [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connectio failed because connected host has failed to respond. Please help. Commented May 25, 2011 at 8:14

1 Answer 1

5
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'))
Sign up to request clarification or add additional context in comments.

7 Comments

finally a new error - [Errno ftp error] [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connectio failed because connected host has failed to respond
The URL is an example. If you want someone to code up a complete program for you, you should perhaps consider hiring someone to do so.
It gives different errors for different URLs man... I don't know what to do.
I have just successfully retrieved Google's logo from google.com/images/logos/ps_logo2.png But Zach, there are no oracles here to guess, what are you trying to retrieve and which kind of errors you get. Any details on "different errors"?
Besides I've posted the question to learn what I'm doing wrong. Isn't that what StackOverflow's about ? I respect you guys for answering my queries so many times...it would be an understatement to say that you saved my life a couple of times too. But I detest how you discourage my curiosity. It's a shame.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.