0

This code keep giving me error that it is an invalid argument. I don't know why it doesn't working

path='https://resources.oreilly.com/examples/0636920023784/raw/master/pydata-book-master/ch02/usagov_bitly_data2012-03-16-1331923249.txt'
f=open(path)
f.readline()

Traceback (most recent call last) in 1 path='https://resources.oreilly.com/examples/0636920023784/raw/master/pydata-book-master/ch02/usagov_bitly_data2012-03-16-1331923249.txt' ----> 2 f=open(path) 3 f.readline()

OSError: [Errno 22] Invalid argument: 'https://resources.oreilly.com/examples/0636920023784/raw/master/pydata-book-master/ch02/usagov_bitly_data2012-03-16-1331923249.txt'

Ghassen
  • 764
  • 6
  • 14
yera
  • 1
  • 1
    Possible duplicate of [In Python, given a URL to a text file, what is the simplest way to read the contents of the text file?](https://stackoverflow.com/questions/1393324/in-python-given-a-url-to-a-text-file-what-is-the-simplest-way-to-read-the-cont) – Ricky Kim Jul 04 '19 at 17:53

1 Answers1

1

That's not the correct way of importing the file. Because the file is being hosted, you'll have to reach the file by making a HTTP Request.

The way to go is by either using urllib3 or requests.

import requests

text = requests.get("<your url>").text

# Then it's up to you...

Or you can download the file itself and use file handling, but I guess that's impractical.

Carlos Damázio
  • 104
  • 1
  • 5