0
import os
import urllib.request 


foldername="test_1"
os.makedirs(str(foldername))
tempurl=r"http://img2.wikia.nocookie.net/__cb20130802012237/camp-tv-series/images/thumb/e/e5/Adam.jpg/250px-Adam.jpg"

fullpath1=r"C:\Users\\files"+foldername
urllib.request.urlretrieve(tempurl, fullpath1)

This code is creating the folder but not retriving the file. if I give obsolute path instead of fullpath it works. am I missing anything

Initially I gave a test url. now I am giving an example of a obsolute url

7
  • your url is incorrect Commented Nov 28, 2019 at 22:17
  • I gave a test url. not a real url Commented Nov 28, 2019 at 22:19
  • 1
    why are you doing str(foldername) when it's already a string? Commented Nov 28, 2019 at 22:28
  • 2
    Did you add the path separator (e.g. "\\") before the foldername? Commented Nov 28, 2019 at 22:28
  • 2
    Also I suggest using docs.python.org/3.5/library/os.path.html#os.path.join instead of a hard coded path separator. Commented Nov 28, 2019 at 22:29

2 Answers 2

1

The problem is that you're doing urllib.request.urlretrieve(tempurl, fullpath1) where fullpath does not specify the actual file being retrieved, change this to urllib.request.urlretrieve(tempurl, os.path.join(fullpath1,"adam.jpg"))

Furthermore, you're using relative path to create the directory then you use absolute path to reference it? Stick to one. In addition to this, you're converting a string to a string? Don't do that, it's already a string. Also, avoid using hardcoded paths. Full solution:

import os
from urllib.request import urlretrieve as get

foldername="test_1"
os.makedirs(foldername, exist_ok=True)
url="http://img2.wikia.nocookie.net/__cb20130802012237/camp-tv-series/images/thumb/e/e5/Adam.jpg/250px-Adam.jpg"
get(url, os.path.join(foldername,"adam.jpg"))
Sign up to request clarification or add additional context in comments.

Comments

0

try this code it's working, you need to use a correct name

import os
import urllib.request


foldername ="tesd"
os.makedirs(str(foldername)



tempurl =r"https://f1.media.brightcove.com/8/1078702682/1078702682_6004950245001_6004956161001-vs.jpg"

archive_path, headers = urllib.request.urlretrieve(tempurl ,foldername+"/"+str(tempurl.split("/")[-1]), data=None)

1 Comment

the problem is in the name of the downloaded file so i removed the full path and let juste the name of file

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.