Open file from Google Drive on Mac in Python and using generic path

219 views Asked by At

I'm trying to open a file from my Google Drive locally using a generic path name and without using the Google API to access the drive (the setup is difficult in my organisation).

I've tried to use path = '~/Google Drive/Shared drives/ as the generic path name, which works when reading in pandas dataframes using pd.read_csv(), but it does not work when I use open(path + filename, 'r') for other files. My understanding is that this is because if the path does not start with a / then it assumes you are looking for it locally.

I've also tried using abspath(expanduser("~/"), however this doesn't work either as my username is attached to the google drive folder as such /Users/first.last/CloudStorage/[email protected]/Shared drives/, and exapanding the ~ will only give me /Users/first.last/.

Is there anyway to expand ~/Google Drive/ into the full path name, or is there any other way to access this path generically?

2

There are 2 answers

0
Alex On

~ is literally your user's home folder like /Users/first.last/. Its the same things. What do you mean by expand ~/Google Drive/??? It would be /Users/first.last/Google Drive/. There is no mistake.

Maybe ~/Google Drive/ is LINK to /Users/first.last/CloudStorage/[email protected]/Shared drives/ and actually there is no such folder.

Can you go to folder manually. Choose file and look for absolute path in properties?

As J_H already said looks like you are looking for "~/CloudStorage/[email protected]/Shared drives/"

3
J_H On

Is there any way to expand "~/Google Drive/" into the full path name

Yes.

Use expanduser().

from pathlib import Path

folder = Path("~/Google Drive/").expanduser()
print(folder)

It sounds like what you really want may be

folder = Path("~/CloudStorage/[email protected]/Shared drives/").expanduser()