7

I have a Jupyter notebook and I have written codes to work with data. Now I want to use Google Colab for it's GPU computation power, so I need to read from and write to local files in my computer from Colab. I don't want to select file manually using:

from google.colab import files
uploaded = files.upload()

mentioned in this link where a "select file" pop up will appear, I want this action to be automatically. Let me clarify that:
I have code below where I read data from a local file:

# Reading the csv file and convert it to a dataframe using pandas library
train_set = pd.read_csv('Datasets/train.csv')

Then I per-process data and output the result in another file. To do that, I need to open a file, read the content of it which is the version of my created output file, and then save the output file:

output_version = open('OutputVersion','r+')
version = output_version.read()

training_set.to_csv('Datasets/training_set_processed_{}.csv'.format(version))

So, I have to read, manipulate and write files in my local storage from Colab automatically. How can I do that? I have read this manual but I couldn't get that. Again, I don't want a 'select file' dialogue pop-up.

Thanks in Advance

3 Answers 3

7

You might want to use the file browser in the left hand side in order to upload files.

enter image description here

enter image description here

If you have a lot of files, a simpler method still is to mount your Google Drive in the filesystem.

1
  • As I have mentioned in the question, I don't want this action to be manually and using a dialogue box. Files are changing as data is preprocessed in a different way and I want the Colab to read and write file automatically. Commented Nov 8, 2018 at 6:09
1

In Colab when you use the "upload files" option, it stays in memory unless your runtime session is over after that the files you uploaded gets dissolves into nothing.

If you want to do this process automatically you might want to opt for uploading your requisite file in the drive and then mounting the drive in Colab. By using this command:

from google.colab import drive
drive.mount('/content/drive')

It will ask for an authorization code, you can get that by going to the URL it gives you when you wrote the code.
After that, you can call on your files like this:

data = '/content/drive/MyDrive/your_file_path'
1
  • does this mean the 'save' command (at file>save) saves to the original local file that was uploaded? Commented Mar 29, 2023 at 18:22
0

there is a better way in which you can make a folder for google colab in you google drvie and upload all your files there

in colab notebooks you can upload all files and do you work

Not the answer you're looking for? Browse other questions tagged or ask your own question.