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
-
Any possibility of comparing the original file and the download file and seeing what's different? Might give an important clue.martineau– martineau2013-03-02 18:48:41 +00:00Commented Mar 2, 2013 at 18:48
-
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 problemguyzyl– guyzyl2013-03-02 19:22:49 +00:00Commented Mar 2, 2013 at 19:22
-
Are you saying you've done this comparison and there was no discernible pattern in how they differed?martineau– martineau2013-03-02 19:28:56 +00:00Commented Mar 2, 2013 at 19:28
-
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– guyzyl2013-03-02 20:05:55 +00:00Commented Mar 2, 2013 at 20:05
-
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– martineau2013-03-02 21:10:05 +00:00Commented Mar 2, 2013 at 21:10
1 Answer
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.)