'Python directory file issue
My problem is that when downloading an File with the python urllib.request Module(s) that the current File being downloaded is being put in the same directory as the script. I can't seem to find any solution that works, I tied to use the glob module but no succes here.
No glob method
# Importing required libraries
import urllib.request
# Adding information about user agent
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
# setting filename and image URL
filename = 'sunshine_dog.jpg'
image_url = "https://cdn.pixabay.com/photo/2020/02/06/09/39/summer-4823612_960_720.jpg"
# calling urlretrieve function to get resource
urllib.request.urlretrieve(image_url, filename)
Glob method
# Importing required libraries
import urllib.request
import glob
# Adding information about user agent
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
# setting filename and image URL
filename = 'sunshine_dog.jpg'
image_url = "https://cdn.pixabay.com/photo/2020/02/06/09/39/summer-4823612_960_720.jpg"
# calling urlretrieve function to get resource
urllib.request.urlretrieve(image_url, filename)
glob.glob("/home/users/*/*.png")
If i did not succeed in informating you please tell me, Thanks in advance.
Solution 1:[1]
If you want to save the file to a specific directory, use a full path in the second argument to urlretrieve() instead of a bare filename:
urllib.request.urlretrieve(image_url, '/full/path/to/some/image.jpg')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
