Trag0
Trag0

Reputation: 35

How to access a file hosted on a public remote server (python)?

The tsv file I want to read in my python script is hosted at http://afakesite.org/myfile.tsv (manually accessing the URL launches the downloading of the file but I would like to keep it on the server).

I would like to be able to read this file from a python script (for instance hosted on colab or github, so without downloading the file), but I did not find resources to do this.

f = open("http://afakesite.org/myfile.tsv", "r", encoding="utf8") does not work (returns a [Errno 2] No such file or directory).

Thank you in advance!

Upvotes: 0

Views: 1258

Answers (1)

Lev Levitsky
Lev Levitsky

Reputation: 65791

urllib.request.urlopen is like open, but for URLs:

from urllib.request import urlopen
with urlopen("http://afakesite.org/myfile.tsv") as f:
    # read f like a file object

Note that the object produces bytes, i.e. behaves like a file opened in binary mode. Thus, you can't read it line by line, rather in chunks of certain size.

If the file is not too big, you can read it all at once:

lines = f.read().decode('utf-8').split('\n')

Upvotes: 2

Related Questions