How to determine the Dropbox folder location programmatically?
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.
def _get_appdata_path(): import ctypes from ctypes import wintypes, windll CSIDL_APPDATA = 26 _SHGetFolderPath = windll.shell32.SHGetFolderPathW _SHGetFolderPath.argtypes = [wintypes.HWND, ctypes.c_int, wintypes.HANDLE, wintypes.DWORD, wintypes.LPCWSTR] path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH) result = _SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, path_buf) return path_buf.valuedef dropbox_home(): from platform import system import base64 import os.path _system = system() if _system in ('Windows', 'cli'): host_db_path = os.path.join(_get_appdata_path(), 'Dropbox', 'host.db') elif _system in ('Linux', 'Darwin'): host_db_path = os.path.expanduser('~' '/.dropbox' '/host.db') else: raise RuntimeError('Unknown system={}' .format(_system)) if not os.path.exists(host_db_path): raise RuntimeError("Config path={} doesn't exists" .format(host_db_path)) with open(host_db_path, 'r') as f: data = f.read().split() return base64.b64decode(data[1])
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:
import osfrom pathlib import Pathimport jsontry: json_path = (Path(os.getenv('LOCALAPPDATA'))/'Dropbox'/'info.json').resolve()except FileNotFoundError: json_path = (Path(os.getenv('APPDATA'))/'Dropbox'/'info.json').resolve()with open(str(json_path)) as f: j = json.load(f)personal_dbox_path = Path(j['personal']['path'])business_dbox_path = Path(j['business']['path'])
You could search the file system using os.walk
. The Dropbox folder is probably within the home directory of the user, so to save some time you could limit your search to that. Example:
import osdropbox_folder = Nonefor dirname, dirnames, filenames in os.walk(os.path.expanduser('~')): for subdirname in dirnames: if(subdirname == 'Dropbox'): dropbox_folder = os.path.join(dirname, subdirname) break if dropbox_folder: break# dropbox_folder now contains the full path to the Dropbox folder, or# None if the folder wasn't found
Alternatively you could prompt the user for the Dropbox folder location, or make it configurable via a config file.