编辑
这个问题已经回答了,我不会接受任何新的答案。
结束编辑
注意:在不同的编程语言中都有类似的问题,但是它们没有专门针对dropbox库(或者至少我找不到),这就是我创建这个问题的原因。
我想知道如何使用Python2.7中的dropbox库将一个文件上传到我的Dropbox并读取回一个文件。
我已经成功地连接到我的Dropbox,Dropbox对象被称为db。
如果有人知道如何做到这一点,请写一个包含方法调用和参数的答案,或者如果这是一个重复的问题,请与链接评论。
提前谢谢你!
发布于 2017-05-01 18:38:22
Dropbox Python为向后兼容性提供了API v1和API v2功能,但是现在应该只使用API v2,因为API v1是已弃用。教程涵盖了使用API v2功能的基础。
这使用Dropbox Python从远程路径/Homework/math/Prime_Numbers.txt的Dropbox下载一个文件到本地文件Prime_Numbers.txt。
import dropbox
dbx = dropbox.Dropbox("<ACCESS_TOKEN>")
with open("Prime_Numbers.txt", "wb") as f:
metadata, res = dbx.files_download(path="/Homework/math/Prime_Numbers.txt")
f.write(res.content)应该用访问令牌替换<ACCESS_TOKEN>。
以及上传:
这使用Dropbox Python将文件从file_path指定的本地文件上传到dest_path指定的远程路径。它还根据文件的大小选择是否使用上载会话:
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 4 * 1024 * 1024
if file_size <= CHUNK_SIZE:
print dbx.files_upload(f.read(), dest_path)
else:
upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
offset=f.tell())
commit = dropbox.files.CommitInfo(path=dest_path)
while f.tell() < file_size:
if ((file_size - f.tell()) <= CHUNK_SIZE):
print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
cursor,
commit)
else:
dbx.files_upload_session_append(f.read(CHUNK_SIZE),
cursor.session_id,
cursor.offset)
cursor.offset = f.tell()
f.close()发布于 2017-04-30 17:49:06
Note
这是API v1,它现在被废弃了。小心使用或使用当前支持的API。
初始化Dropbox客户端
import dropbox
access_token = 'SOME_ACCESS_TOKEN'
client = dropbox.client.DropboxClient(access_token)上传文件
src_file = open('SOME_LOCAL_FILE', 'r')
response = client.put_file('SOME_REMOTE_FILE', src_file)下载文件
dest_file = open('SOME_LOCAL_FILE', 'w')
with client.get_file('SOME_REMOTE_FILE') as src_file:
dest_file.write(src_file.read())参考
有关更简洁的API文档,请参阅Python文档的核心API。
发布于 2017-09-30 12:29:30
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 4 * 1024 * 1024
if file_size <= CHUNK_SIZE:
print dbx.files_upload(f, dest_path)
else:
upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell())
commit = dropbox.files.CommitInfo(path=dest_path)
while f.tell() < file_size:
commit = dropbox.files.CommitInfo(path=dest_path)
if ((file_size - f.tell()) <= CHUNK_SIZE):
print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
cursor,
commit)
else:
dbx.files_upload_session_append(f.read(CHUNK_SIZE),cursor.session_id, cursor.offset)
cursor.offset = f.tell()https://stackoverflow.com/questions/43709247
复制相似问题