Reputation: 52218
I need the file path for a file stored in Google drive, so I can access it from a Google colab notebook.
E.g.
my_dat = ZipFile('/content/drive/MyDrive/some/file/structure/dat.zip', 'r')
Is there a quick way to "copy as path" or otherwise get the file path to the clipboard as quickly as possible from google drive?
My current method is to manually type up the file path, which is very tedious when doing it repeatedly for files deep down in directories. Basically all I need is a way to quickly and easily have 'MyDrive/some/file/structure/dat.zip' on my clipboard (e.g. similar to here), just without manually typing it.
Upvotes: 4
Views: 11455
Reputation: 33
You can get the file path of a Google document with Apps Script
function filePath() {
var fileId = 'file ID string goes here';
var parents = DriveApp.getFileById(fileId).getParents();
var parent;
var pathString = DriveApp.getFileById(fileId).getName() ;
while (parents.hasNext()){
parent = parents.next();
pathString = parent.getName() + ">" + pathString;
parents = parent.getParents()
}
Logger.log(pathString)
}
In this example, just copy the file ID from the URL and paste it into the code. The example includes the file name at the end. You can change the separator to whatever you need.
Upvotes: 0
Reputation: 40808
I made my own library to make it easier.
!pip install kora
from kora.drive import get_path
p = get_path(file_id)
Upvotes: 4
Reputation: 116868
The best way or really the only way I know of doing this using the Google drive api would be to use the
file.get method, this will return a field called parents. Once you get the parent id you can then do a file get on the parent and continue up until the parent is root. Its going to mean a lot of calls but its the only way I have found to achieve this.
Upvotes: 2