python - 使用 dropbox API 下载指定文件夹中的所有图像,而不是子文件夹

标签 python json api dropbox dropbox-api

我想编写代码,可以使用 Dropbox API 下载指定文件夹中的所有图像,而不是 Dropbox 中的子文件夹。到目前为止我已经写了这个

 def download_cont(self, folderName):

    fname = folderName
    folder_metadata = self.client.metadata('/' + fname)
    print folder_metadata

这是元数据:

    {u'size': u'0 bytes', u'hash': u'3fad7ce5537e0941f8768413cdb7b84d', u'bytes': 0, u'thumb_exists': False, u'rev': u'111c24338d', u'modified': u'Sun, 22 Dec 2013 02:09:41 +0000', u'path': u'/images', u'is_dir': True, u'icon': u'folder', u'root': u'dropbox', u'contents': [{u'size': u'56.2 KB', u'rev': u'131c24338d', u'thumb_exists': True, u'bytes': 57538, u'modified': u'Sun, 22 Dec 2013 02:16:34 +0000', u'mime_type': u'image/png', u'path': u'/images/296px-Manchester_United_FC_crest.svg.png', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:16:34 +0000', u'revision': 19}, {u'size': u'9.8 KB', u'rev': u'141c24338d', u'thumb_exists': True, u'bytes': 9999, u'modified': u'Sun, 22 Dec 2013 02:16:36 +0000', u'mime_type': u'image/jpeg', u'path': u'/images/images.jpg', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:16:36 +0000', u'revision': 20}, {u'size': u'77 KB', u'rev': u'151c24338d', u'thumb_exists': True, u'bytes': 78798, u'modified': u'Sun, 22 Dec 2013 02:16:39 +0000', u'mime_type': u'image/jpeg', u'path': u'/images/manchester-united-fc-3d.jpg', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:16:39 +0000', u'revision': 21}, {u'size': u'220.9 KB', u'rev': u'121c24338d', u'thumb_exists': True, u'bytes': 226209, u'modified': u'Sun, 22 Dec 2013 02:15:51 +0000', u'mime_type': u'image/jpeg', u'path': u'/images/manchester-united-plc-logo.jpg', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:15:51 +0000', u'revision': 18}, {u'size': u'0 bytes', u'rev': u'161c24338d', u'thumb_exists': False, u'bytes': 0, u'modified': u'Sun, 22 Dec 2013 02:16:53 +0000', u'path': u'/images/sub', u'is_dir': True, u'icon': u'folder', u'root': u'dropbox', u'revision': 22}], u'revision': 17}

据我所知,我必须遍历 JSON 对象并使用元数据中的 'contents' 下载每个文件,但我不确定如何执行此操作。请帮忙。

最佳答案

你是对的。元数据的 contents 成员将告诉您有关列出的文件夹中的文件(和子文件夹)的信息。每个条目都有一个 path 成员(告诉您其完整路径)和一个 is_dir 成员(告诉您该条目是否是目录(而不是文件))。下面是一些使用 Python 列表理解来获取指定文件夹中文件的所有路径,然后下载每个文件的代码:

def download_cont(self, folder_name):
    for path in [entry['path'] for entry in self.client.metadata(folder_name)['contents'] if not entry['is_dir']]:
        name = os.path.basename(path)
        print 'Saving "%s"...' % name
        with open(name, 'wb') as out:
            with self.client.get_file(path) as f:
                out.write(f.read())
<小时/>

更新:如果您没有使用最新 (2.0) 版本的 Dropbox Python SDK,则不应使用嵌套 with 语句。只需这样做:

print 'Saving "%s"...' % name
with open(name, 'wb') as out:
    out.write(self.client.get_file(path).read())

关于python - 使用 dropbox API 下载指定文件夹中的所有图像,而不是子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20727301/

相关文章:

python - 为图像着色 Pygame

ios - 为 iOS 应用程序返回带有丰富标记的文本

php - 创建两个数组之间的关系

c - 以灵活且不显眼的方式在 C 中扩展 API

java - 微调器返回 null

python - 无需手动输入即可读取文件名列表

python - Tensorflow 兼容模块问题?

python - 类型错误 : only length-1 arrays can be converted to Python scalars with NUMPY

ios - Swift:If let 语句无法处理空数组

Azure 消耗使用情况详细信息 API 响应为 "cost":0 and "effectivePrice":0 for every record