0
votes

I am trying to download a file from Dropbox using the python api. I am doing exactly the same as said on their "Getting Started" page https://www.dropbox.com/developers/core/files#python .It works fine for simple text files, but downloads a corrupted file when used for media files (like .mp3, or .jpg). Is there something I am missing, or a different approach to downloading the file? Thanks, Guyzyl

1
Any possibility of comparing the original file and the download file and seeing what's different? Might give an important clue. - martineau
I can compare the original mp3, and the one downloaded from dropbox-api using a hex editor, but that doesn't give me a clue about the problem - guyzyl
Are you saying you've done this comparison and there was no discernible pattern in how they differed? - martineau
There were differences, but that still doesn't help me find the problem, when i dont know what those differences mean. Here are the two files: dropbox.com/s/rrly2tdf0mf866z/%28Original%29.mp3?m dropbox.com/s/9c6ulgpj7lwl64j/… - guyzyl
Sorry, I don't have a decent binary diff utility handy. Look for things like byte-order being reversed, line-ending characters being changed, high bits being stripped, which depending on what you find, would offer clues about what's wrong. Unless somehow uncorrected transmission errors are getting through, you like have some sort of software or configuration error IMO. - martineau

1 Answers

2
votes

The example on the Dropbox page is not optimized for binary files like MP3s or JPGs. You should replace out = open('magnum-opus.txt', 'w') with out = open('magnum-opus.txt', 'wb').

See the Python documentation on the open built-in:

The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.)