0

I'm trying to import an image in a folder in my desktop however I have no clue of how to do it, I find it really difficult to understand the documentations as a beginner.

I already know how to download the file but I don't how I can download it in a certain location.

here is what I have

def download_img(url):
    name = random.randrange(1, 100)
    full_name = str(name) + ".png"
    urllib.request.urlretrieve(url, full_name)

download_img('http://www.mtv.com/crop-images/2013/11/05/the_killers_elle_exclusive_williams_hirakawa.jpg')

how can I download the image in my Desktop, where should I specify the path?

Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
  • @soon just checked, but he doesn't specify, how to change the path where the image would be downloaded. – Horai Nuri Dec 22 '15 at 13:50
  • Please, take a look again at the code sample #4 with the following line: `with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:`. – awesoon Dec 22 '15 at 13:52
  • 1
    @Sia, in the link @soon gave it simply downloads the file and returns its contents. It would be up to the caller to determine where to save it. For that, you would probably use `open`. See here for an example http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/ – criswell Dec 22 '15 at 13:55
  • @criswell from what I understand it change the file name, it doesn't put the file in the file that is on my desktop, I'm not sure of what I am saying, but I don't know how Python will find my folder, if I don't specify it with a path? – Horai Nuri Dec 22 '15 at 13:57
  • File in same directory (script path) if not specify a new path. Maybe has ben change if used an external file maker module. – dsgdfg Dec 22 '15 at 14:01
  • @Sia, it will save the file to whatever path you specify. If you don't specify a path, it will save it to the current working directory of the script (the directory you ran the script from). – criswell Dec 22 '15 at 14:09
  • @criswell yeah, the question where should I specify the path ? – Horai Nuri Dec 22 '15 at 14:11
  • @zetysz it doesn't matter, it will still save it as a .png file – Horai Nuri Dec 22 '15 at 14:12
  • @Sia you specify the path where you specify the file. I should also mention this *isn't* a Python-problem, per se... It's really more of a general programing problem. Most languages treat file-paths the same way (in the absence of a path, they save to current working directory). – criswell Dec 22 '15 at 14:49

2 Answers2

3

Try this:

import urllib.request
import random
import os

def download_img(url, path):
    name = random.randrange(1, 100)
    full_name = str(name) + ".png"
    response = urllib.request.urlopen(url)
    data = response.read()
    f = open(os.path.join(path , full_name), 'wb')
    f.write(data)
    f.close()

download_img('http://www.mtv.com/crop-images/2013/11/05/the_killers_elle_exclusive_williams_hirakawa.jpg', '/tmp')
Kenly
  • 24,317
  • 7
  • 44
  • 60
1

Following will work. Tested it locally.

from urllib import request

# Name of the file you want to download and eventually the file which will be created on your machine
filename = "2ixBxKK.jpg"
response = request.urlopen("http://i.imgur.com/%s" % filename)

myfile = open("/tmp/%s" % filename, "wb")
myfile.write(response.read())

myfile.close()
Jay
  • 1,539
  • 1
  • 16
  • 27