How can i read a txt file from a website in python

Im learning NLTk and i need to load in a large file and i dont want to save it on my desktop How can i read in a file with python thats hosted on a website?

I tried this code here but it didnt work, i assume that the open with is the rson for it but i need to use open with because i need to save it as a file - myfile in this case.

import nltk

with open('http://www.sls.hawaii.edu/bley-vroman/brown.txt', 'r')as myfile:
    data=myfile.read().replace('\n', 'r')

data2 = data.replace("/", "")

for i, in line in enummerate(data2.split('\n')):
    if i>10:
        break
    print(str(i) + ':\t' + line)

and this is the error:

Traceback (most recent call last):
  File "tut1.py", line 3, in <module>
    with open('http://www.sls.hawaii.edu/bley-vroman/brown.txt', 'r')as myfile:
FileNotFoundError: [Errno 2] No such file or directory: 'http://www.sls.hawaii.edu/bley-vroman/brown.txt'

What can i do to use the file in my script without downloading the whole file?

I changed the code to work with requests

import nltk
import requests

myfile = requests.get('http://www.sls.hawaii.edu/bley-vroman/brown.txt')

data=myfile.read().replace('\n', 'r')

but now when i run this i get this error:

Traceback (most recent call last):
  File "tut1.py", line 6, in <module>
    data=myfile.read().replace('\n', 'r')
AttributeError: 'Response' object has no attribute 'read'


Read more here: https://stackoverflow.com/questions/64947319/how-can-i-read-a-txt-file-from-a-website-in-python

Content Attribution

This content was originally published by yappy twan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: