How do I automatically upload file in Google Colab?

Viewed 618

I'm able to manually upload a file to Google Colab using the following code.

1  import json
2  from google.colab import files
3  uploaded = files.upload()

Each time it reaches Line 3, I'll have to manually click Upload File, then select the file locally before the code continues to run. After a few runs, even if I cancel the upload at Line 3, it seems to run just fine without uploading the file.

How do you automatically upload the same file (stored locally) each time you run the code in Google Colab? Or should I store it in Google Drive and link the file's URL? If so, how would I do that in the code?

1 Answers

I had similar issues, I was able to upload /link files in Google Colab

'''store a file in google drive and create a shareable link, change "file/d" to "uc?export=download&" and remove /view?usp=sharing. 
This will create a link that you can store in a variable example below'''
#link = 'https://drive.google.com/file/d/1n2ZS50c_WbHBK9gIco4gRAUSm9uWfo3Q/view?usp=sharing'
# replace 'file/d/' with 'uc?export=download&id=' and remove '/view?usp=sharing'
#link = 'https://drive.google.com/uc?export=download&id=1n2ZS50c_WbHBK9gIco4gRAUSm9uWfo3Q'

'''Direct file link can also be saved to variable as below '''
# User inputs
fileLink = "https://raw.githubusercontent.com/pavanghai/DataSets/master/games.csv"
fileType = "csv"
fileLink1 = 'https://drive.google.com/uc?export=download&id=1n2ZS50c_WbHBK9gIco4gRAUSm9uWfo3Q'

or you can create a download file as below

# Download a filelink as file in google root folder "/content/"
!wget https://raw.githubusercontent.com/pavanghai/DataSets/master/games.csv -q

-q is quite mode

Related