Vincent Stevenson
Vincent Stevenson

Reputation: 53

Python Dropbox - How to create a directory in Dropbox and Upload to that new folder?

How can I use the Python Dropbox object to create a new folder under root and then populate that folder with data?

How can I create a method in Python to send data into a specific folder within a Dropbox app? When I run the below code:

def ship_data_to_dbx(job_id, mn):
    # ship db and zip and txt to dir for the machine name in dropbox
    mn = mn.upper()
    # specific db path for the machine
    dbx_path = "/" + mn + "/"
    db_path = os.path.join(app.root_path, 'static/log_files', job_id + ".db")
    with open(db_path, "rb") as f:
        dbx.files_upload(f.read(), dbx_path, mute=True)

I get this error message:

dropbox.exceptions.ApiError: ApiError('8e663db4b9ae4d00b954f97065393160', UploadError('path', UploadWriteFailed(reason=WriteError('malformed_path', None), upload_session_id='AAAAAAAAAoBUOebujJCC_A')))

Thanks!

Upvotes: 1

Views: 2421

Answers (1)

Greg
Greg

Reputation: 16930

To create a new folder via the Dropbox API using the official Dropbox API v2 Python SDK, you should use the files_create_folder_v2 method.

To upload a file, you can use the files_upload method.

The malformed_path error you're getting when attempting to do so indicates that the path value you're supplying to files_upload, in this case, your dbx_path variable, is not in the expected format.

In particular, while the exact value isn't shown, it looks like the issue is that your dbx_path value has a trailing "/", which isn't expected. When specifying the path to upload to in Dropbox, you should include the full desired path, including any parent path components, as well as the file name, and also extension (if any).

So, for example, if your mn variable contains the filename that you want the uploaded file to have, that line should instead be:

dbx_path = "/" + mn

Or, if mn is the parent folder, it would look something like this, with filename containing the name and extension of the file:

dbx_path = "/" + mn + "/" + filename

Upvotes: 1

Related Questions