Jainmiah
Jainmiah

Reputation: 467

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)

Upvotes: 4

Views: 9732

Answers (3)

boozy
boozy

Reputation: 323

Even though @korakot's response is valid, as an alternative solution:

!git clone https://github.com/Jainu-s/urldata.git
path = '/content/urldata/al'

%cd urldata #go to the directory where git clone says *Cloning into*

for subdir, dirs, files in os.walk(path):
  print(files)

Upvotes: 1

korakot
korakot

Reputation: 40798

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.

Upvotes: 4

Suraj
Suraj

Reputation: 2477

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.

Upvotes: 7

Related Questions