Welcome folks today in this post we will be talking about how to
download any file from url in python using
google colab to your
google drive. All the source code regarding this project will be shown below.
In order to get started you must be having a
google account to use google colab feature.
Google Colab is a google technology online which allows you to execute python code inside your browser for free.
Now to get started you need to create a new
workbook as shown in the figure. First go to Google Colab here.
Now you need to write some code inside this workbook. Just copy paste the code which is shown below.
from google.colab import drive drive.mount('/content/gdrive')
Here just copy paste the above code inside the
google colab editor like this as shown below
Now if you run this code by pressing the
triangle button which is there on left side you will see the following result which is shown below
Now you need to go to this above url and grant permissions to use
google drive and then copy paste the
authorization code inside this text field and then press enter
So you can see it has successfully mounted all my
google drive files on the left hand side now we have the full access to do anything with our google drive such as upload any file, download any file to drive, remove files etc.
Now just write code to
download file from url to drive using colab. Just copy paste the following code which is shown below
import requests file_url="http://www.africau.edu/images/default/sample.pdf" r = requests.get(file_url, stream = True) with open("/content/gdrive/My Drive/python.pdf", "wb") as file: for block in r.iter_content(chunk_size = 1024): if block: file.write(block)
Now you can see after executing this code a
python.pdf has been successfully downloaded from the url and stored it inside our google drive.