> For the complete documentation index, see [llms.txt](https://yo-sarawut.gitbook.io/snippet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yo-sarawut.gitbook.io/snippet/python/download-file.md).

# Download file

* <https://www.codegrepper.com/search.php?q=python%20download%20file%20from%20url>

### Download Image

```python
import urllib.request
imgURL = "http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg"

urllib.request.urlretrieve(imgURL, "D:/abc/image/local-filename.jpg")
```

```python
import wget

url = "https://www.python.org/static/img/python-logo@2x.png"

wget.download(url, 'c:/users/LikeGeeks/downloads/pythonLogo.png')
```

### Download file from url

```python
import requests


url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url, allow_redirects=True)

open('facebook.ico', 'wb').write(r.content)
```

### Download PDF

```python
import urllib.request
pdf_path = ""
def download_file(download_url, filename):
    response = urllib.request.urlopen(download_url)    
    file = open(filename + ".pdf", 'wb')
    file.write(response.read())
    file.close()
 
download_file(pdf_path, "Test")
```

### Text file

```python
import urllib.request as urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()
print(data)
```

```python
import urllib.request as urllib2  # the lib that handles the url stuff

data = urllib2.urlopen(target_url) # it's a file like object and works just like a file
for line in data: # files are iterable
    print line
```
