boberdorf
boberdorf

Reputation: 133

Accessing a folder in Google Drive from Colaboratory

I want to be able to read and write files to a folder in Google Drive from Colaboratory but cannot figure how to access a specific folder.

I successfully mounted my drive, and then did a file listing. The listing shows the directory I am interested (edited for brevity), so I attempt to change to the directory but get an error.

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

    Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True)

import os
os.listdir()

Output (edited)

['99 Temp', '02 Work', '01 Home', 'Home-Building-Key-Facts-Sheet.pdf', 'Home-Contents-Key-Facts-Sheet.pdf', ....., 'Travel Insurance Allianz Certificate of Insurance.pdf', 'Data']

os.chdir("drive/Data")
os.listdir()

*FileNotFoundError                         Traceback (most recent call last)
<ipython-input-25-853a98f4629a> in <module>()
----> 1 os.chdir("drive/Data")
      2 os.listdir()
FileNotFoundError: [Errno 2] No such file or directory: 'drive/Data'*

Upvotes: 0

Views: 6137

Answers (2)

Amit Vashistha
Amit Vashistha

Reputation: 260

My answer is based on assumption that the 'Data' folder in Google Drive is saved under My Drive folder (which is normal by default for Google Drive)

import os
os.chdir("/content/drive/My Drive/Data")

!pwd

Upvotes: 1

Kyohei
Kyohei

Reputation: 161

If you want to move to the 'Data' directory listed in the last element of your edited output, please try this. It looks like you are already in /content/drive.

os.chdir("Data")
os.listdir()

You can confirm your current working directory by pwd command.

!pwd

Upvotes: 1

Related Questions