Python 3.x Download a file from the internet in python?

Python 3.x Download a file from the internet in python?,python-3.x,python-requests,filepath,Python 3.x,Python Requests,Filepath,Any inputs?

Any inputs?


You are opening a requests object not a file path

file_url = requests.get(downloadUrl)
with open(file_url, 'w+') as f:

You would need to open a filepath on disk somewhere and write to that

See Using the request Module on

EDIT: From comments on post

In [584]: link = ""

In [585]: import requests

In [586]: file_url = requests.get(link)

In [587]: with  open('/tmp/NASDAQ_QFIN_2018.pdf', 'wb') as f:
    ...:     f.write(file_url.content)

In [588]: ls -al /tmp/NASDAQ_QFIN_2018.pdf
-rw-r--r--  1 ME  wheel  1796199  9 Apr 15:21 /tmp/NASDAQ_QFIN_2018.pdf


This looks quite similar to this other recent question.


@GinoMempin yeah! but it is not answered, so I still need an answer sir, Thank you


i think you mean with open(filepath, 'w+') as f: as file_url contains response and not the url of the file you want to create.


Thank you for clarification, but I'm getting a PermissionError: [Errno 13] Error after adding with open(filepath, 'w+') as f:


That is because you are trying to open a Directory not a file you would need to download to C:\Users\PacY\Downloads\myfilename.whatever


Okay. I have done it. Now I can enter a path as userinput refer to edited codes above. the script will download the file in the specified path, but the problem the downloaded file will be empty. 0KB


What is an example URL you are trying to download?