I have a script that is intended to be run by multiple users on multiple computers, and they don't all have their Dropbox folders in their respective home directories. I'd hate to have to hard code paths in the script. I'd much rather figure out the path programatically.
Any suggestions welcome.
EDIT: I am not using the Dropbox API in the script, the script simply reads files in a specific Dropbox folder shared between the users. The only thing I need is the path to the Dropbox folder, as I of course already know the relative path within the Dropbox file structure.
EDIT: If it matters, I am using Windows 7.
Note: requires Dropbox >= 2.8
Dropbox now stores the paths in json format in a file called
info.json
. It is located in one of the two following locations:I can access the
%APPDATA%
environment variable in Python byos.environ['APPDATA']
, however I check both that andos.environ['LOCALAPPDATA']
. Then I convert the JSON into a dictionary and read the'path'
value under the appropriate Dropbox (business or personal).Calling
get_dropbox_location()
from the code below will return the filepath of the business Dropbox, whileget_dropbox_location('personal')
will return the file path of the personal Dropbox.This is a pure Python solution, unlike the other solution using
info.json
.This should work on Win7. The use of
getEnvironmentVariable("APPDATA")
instead ofos.getenv('APPDATA')
supports Unicode filepaths -- see question titled Problems with umlauts in python appdata environvent variable.This adaptation based on J.F. Sebastian's suggestion works for me on Ubuntu:
And to actually set the working directory to be there:
One option is you could go searching for the
.dropbox.cache
directory which (at least on Mac and Linux) is a hidden folder in the Dropbox directory.I am fairly certain that Dropbox stores its preferences in an encrypted
.dbx
container, so extracting it using the same method that Dropbox uses is not trivial.I found the answer here. Setting
s
equal to the 2nd line in~\AppData\Roaming\Dropbox\host.db
and then decoding it with base64 gives the path.There is an answer to this on Dropbox Help Center - How can I programmatically find the Dropbox folder paths?
Short version:
Use
~/.dropbox/info.json
or%APPDATA%\Dropbox\info.json
Long version:
Access the valid
%APPDATA%
or%LOCALAPPDATA%
location this way: