aj31
aj31

Reputation: 187

Downloading image from DropBox using Python

I am trying to download an image from dropbox to my desktop using Python. The script below runs to completion without issues and creates a JPEG file on the desktop (about 200+ KB in size). But when I try to open it, I get a file damaged / Preview cannot read file error message:

import requests
from requests.auth import HTTPBasicAuth
import shutil


url = 'https://www.dropbox.com/rest_of_the_url'
db_username = 'user_name'
db_password = 'password'

downloaded_file = requests.get(url, auth=HTTPBasicAuth(db_username, db_password))

dest_file = open('/Users/aj/Desktop/test.jpg', 'w+')

dest_file.write(downloaded_file.content)

What am I doing wrong here?

EDIT: Found the solution. It had to do with the 'dl' parameter in the dropbox link. This parameter needs to be set to 0.

Original link:

https://www.dropbox.com/s/3xujisscbp92to/2.jpg?dl=0

Need to set the dl parameter to 1:

https://www.dropbox.com/s/3xujisscbpj92to/2.jpg?dl=1

Upvotes: 2

Views: 2377

Answers (1)

aj31
aj31

Reputation: 187

Found the solution. It had to do with the 'dl' parameter in the dropbox link. This parameter needs to be set to 0.

Original link:

https://www.dropbox.com/s/3xujisscbp92to/2.jpg?dl=0

Need to set the dl parameter to 1:

https://www.dropbox.com/s/3xujisscbpj92to/2.jpg?dl=1

Upvotes: 3

Related Questions