python - 如何以编程方式确定 Dropbox 文件夹位置?

标签 python directory dropbox

我有一个脚本供多个用户在多台计算机上运行,​​但他们的 Dropbox 文件夹并不都位于各自的主目录中。我不想在脚本中硬编码路径。我宁愿以编程方式找出路径。

欢迎提出任何建议。

编辑: 我没有在脚本中使用 Dropbox API,脚本只是读取用户之间共享的特定 Dropbox 文件夹中的文件。我唯一需要的是 Dropbox 文件夹的路径,因为我当然已经知道 Dropbox 文件结构中的相对路径。

编辑: 如果重要的话,我使用的是 Windows 7。

最佳答案

我找到了答案 here .设置 s 等于 ~\AppData\Roaming\Dropbox\host.db 中的第二行,然后用 base64 解码得到路径。

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.value

def 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])

关于python - 如何以编程方式确定 Dropbox 文件夹位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12118162/

相关文章:

python - python 如何以不同的时间间隔递增?

c++ - 如何使用 C++ 和 winAPI 检查目录是否存在

image - 如何在不编辑配置设置的情况下在命令行上指定docker镜像路径?

ios - Cocoapods 每次都需要通过 dropbox 同步在不同的计算机上更新 bolts 框架

python - 在数据文件的文件夹上运行 4 个并发的 python 脚本实例

python - 使用@font face 时在 QtWebEngine 中忽略 Google 字体 (ttf)

ios - 将文件从 iOS 应用程序上传到单个保管箱帐户

javascript - 添加许多具有不同扩展选项的保管箱选择器不起作用

python - PyQt5:如何为每个角色设置自定义鼠标指针?

HTML如何获取此文件夹