1

I am new to Python and don't have experience of advanced techniques in Google Colab. I have been trying to write a Python code where I want to read a text file or MATLAB variable from my Google Drive. Then I would do analysis on that data. I followed the answer of this LINK to mount my drive. I am providing my code and screenshots below to elaborate.

My desired data files (.txt or .mat) are kept in folder named "Wave_Codes" in my Google Drive. I created a folder named "CodeTesting" inside the default folder "Colab Notebooks". There I saved the ipynb code.

from google.colab import drive
drive.mount('/content/drive', force_remount=True)

import sys
import os

path = os.path.abspath("/content/drive/MyDrive/Wave_Codes/")
sys.path.append(path)

After ruuning this code, the ouput shows that the monuting has been done. However, I notice that the File Browser (in Fig 1 below) is not showing the drive being mounted. If I hover my mouse pointer there, it still shows click to mount. Fig 1 This is why I am doubtful if the mount is even getting done at all. When I ran the code, it did ask me to verify my google log in and permission related stuff. I made sure that I did those right.

Next, if I move on to read my data from the file. It is showing error of "File Not Found" (Fig 2). Fig 2 Error

The only way I managed to read the file is when I put the entire path in the code. Fig 3

So, what is going wrong here? Is the mounting happening properly? Is it an issue of the path not getting set? How do I fix this?

I tried to reset everything, closed the browser, later I turned on the 'force mount' option. Nothing worked. It shows Mount is done, but the folder logo in file browser doesn't change and I keep getting the error while reading the file. I tried to read some simple text files instead of a .mat file, but it still gave me File Not Found error.

UPDATE: After I ran the mounting and path setting code, I ran print(os.getcwd()) as suggested in comments. This revealed that the working directory is "content" (see Fig 4). Fig 4: Working directory showing as 'content' even after mounting. Does this mean the mount hasn't worked in the first place? Why is this happening? What am I doing wrong?

  • What is your working directory? (You can check with `print(os.getcwd())`.) What you are attempting in Fig 2 would only work if the working directory was `/content/drive/MyDrive/Wave_Codes`. – slothrop Jun 22 '23 at 08:40
  • @slothrop I checked the working directory after running the code I mentioned in my question. Unfortunately, it only shows: `/content` . So, doesn't this mean that even the mounting didn't work? – Wave_Rider03 Jun 22 '23 at 15:18
  • The mounting did work, otherwise you would not be able to access the file at all in Figure 3. I think you are expecting that `sys.path` is used when opening files? But it's only used when importing modules. When you open a file with a relative path, the path is always relative to `os.getcwd()` and `sys.path` is not involved. So your options are (1) just do what you're doing in Fig 3 using an absolute path, or (2) use `os.chdir()` to set your working directory as you desire. – slothrop Jun 22 '23 at 15:26
  • 1
    @slothrop Ok, now I understand. I tried the ```os.chdir()``` way and it works. After using that, I don't need to write the whole path of the file, just mentioning the name works. – Wave_Rider03 Jun 25 '23 at 12:46
  • Also, I guess that folder icon in File Browser showing the mount option is like a visual glitch then since the mount is working? – Wave_Rider03 Jun 25 '23 at 12:49
  • Good question about the File Browser icon - I must admit I'm not sure how that works. – slothrop Jun 25 '23 at 14:37

0 Answers0