How to download file for Python from the Internet for the Python for Everyone class?

624 Views Asked by At

I am currently learning from the Python for Everyone class taught by Dr. Charles Severance on Coursera. https://www.py4e.com/code3/mbox.txt

For chapter 7, the unit on files, I do not understand how I'm supposed to download this file.

Furthermore, now that I've downloaded, I do not know how to open it.

I am using Google colaboratory.

I downloaded the file as "mbox.webarchive" in my downloads, yet this code:

fhand=open("mbox.webarchive")

returns an error:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-95f366aabb43> in <module>()
----> 1 fhand=open("mbox.webarchive")

FileNotFoundError: [Errno 2] No such file or directory: 'mbox.webarchive'

Please advise. I've really been struggling with this. Thank you.

1

There are 1 best solutions below

0
On

You can download from a browser by going to that address https://www.py4e.com/code3/mbox.txt . Or you can just open it directly by using the requests library like the example below:

import requests
response = requests.get('https://www.py4e.com/code3/mbox.txt')

count=0
for line in response:
    count=count+1

 print(count)