gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:09:33 24 4
gpt4 key购买 nike

我有一个脚本供多个用户在多台计算机上运行,​​但他们的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com