Ninekay
Ninekay

Reputation: 35

Error when uploading a directory to Dropbox including all files and sub-directories with Dropbox Python API

I’m trying to upload a directory to Dropbox along with all its files including any sub-directories and its files using the Dropbox Python API.

Files can be uploaded alright and it can create new Dropbox folders. I wonder if anyone has a solution for this.

Error message:

Traceback (most recent call last):
File "dir_2_dropbox.py", line 27, in
client.put_file(dropbox_path, f)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/client.py", line 377, in put_file
return self.rest_client.PUT(url, file_obj, headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 321, in PUT
return cls.IMPL.PUT(*n, **kw)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 258, in PUT
return self.request("PUT", url, body=body, headers=headers, raw_response=raw_response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 227, in request
raise ErrorResponse(r, r.read())
dropbox.rest.ErrorResponse: [400] u'The file /mobistudios/.DS_Store is on the ignored file list, so it was not saved.'

local_dir = ('/users/ninekay/desktop/scripts')

for root, dirs, files in os.walk(local_dir):

    for filename in files:

        local_path = os.path.join(root, filename)

        relative_path = os.path.relpath(local_path, local_dir)
        dropbox_path = os.path.join(‘/mobistudios’, relative_path)

        with open(local_path, 'rb') as f:
            client.put_file(dropbox_path, f)

Why do I have this error and how can I solve it?

Upvotes: 2

Views: 1766

Answers (1)

Ben Beirut
Ben Beirut

Reputation: 763

Check the Ignored files section from Dropbox’s help center.

You could then exclude such files when sync’ing. I add the IGNORED_FILES variable to account for such files and a is_ignored function to check whether files will cause that error before ‘putting’ them.

IGNORED_FILES = ['desktop.ini', 'thumbs.db', '.ds_store',
                         'icon\r', '.dropbox', '.dropbox.attr']
def is_ignored(filename):
    filename_lower = filename.lower()
    for ignored_file in IGNORED_FILES:
        if ignored_file in filename_lower:
            return True
    return False


local_dir = ('/users/ninekay/desktop/scripts')

for root, dirs, files in os.walk(local_dir):

for filename in files:
    if is_ignored(filename):
        continue

    local_path = os.path.join(root, filename)
    relative_path = os.path.relpath(local_path, local_dir)
    dropbox_path = os.path.join(‘/mobistudios’, relative_path)


    with open(local_path, 'rb') as f:
        client.put_file(dropbox_path, f)

Upvotes: 2

Related Questions