Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read .txt files from github into Google colab

I have folder in github which contains text files and when I tried to read below code in Google colab I'm getting error

FileNotFoundError: [Errno 2] No such file or directory: 'https://github.com/Jainu-s/urldata/tree/master/al?raw=true'

loc = 'https://github.com/Jainu-s/urldata/tree/master/al?raw=true'
#uploaded = files.upload()
os.chdir(loc)
filelist = os.listdir()
#print (len((pd.concat([pd.read_csv(item, names=[item[:-4]]) for item in filelist],axis=1))))

data = []
path = loc
files = [f for f in os.listdir(path) if os.path.isfile(f)]
for f in files:
    with open(f,'r') as myfile:
        data.append(myfile.read())


df = pd.DataFrame(data,columns=['Data'])
print (df.shape)
like image 888
Jainmiah Avatar asked Sep 05 '25 03:09

Jainmiah


2 Answers

import base64
import requests

master = "https://raw.githubusercontent.com/Jainu-s/urldata/master/al/abescoldbeer.com.txt"
req = requests.get(master)
req = req.text
print(req)

In this way you can read all the files using a for loop modifying the master string

https://stackoverflow.com/a/38497199/10077354 You can refer this link to know about reading github files.

like image 54
Suraj Subramanian Avatar answered Sep 08 '25 20:09

Suraj Subramanian


You can download all files in that directory to Colab first with:

!npx degit Jainu-s/urldata/al -f

Then, you can loop it like local files.

like image 37
korakot Avatar answered Sep 08 '25 18:09

korakot