3

The following snippet in my google colab:

import sys
import os

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

path = '/content/gdrive/MyDrive/MBA/MBA_USP/03_Modulo_Solucoes/16_Metologia_Pesquisa_Projeto_Conclusao/TCC'
fnames = os.listdir(path)

prints my files and directories:

['TCC', 'degree_dist.eps', 'mind_dataset']

Now I'd like to search for files starting from the above path, like so:

if os.path.exists('mind_dataset/file.csv'):
    try:
        # do something

But this is not finding file.csv, which is there. How do I append or join paths to my present path in colab, in order to find any file?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

1 Answers1

0

This seem related to setting up a relative path for Google Colab environment.

One way of doing so is by setting up a relative path to the desired folder before running any code (or just putting the absolute path)

%cd <path of parent directory>
   
if os.path.exists('mind_dataset/file.csv'):
    try:
        # do something

When running drive.mount('/content/drive') the environment path is /content but you can navigate to others existing directories

%pwd               # prints out the current environment path
os.listdir('.')    # list all files and folder in the current environment
%cd <desired path> # sets up a relative path

this works for both a local jupyter notebook or a Google Colab one