问题
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)
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/61723047/how-to-read-txt-files-from-github-into-google-colab