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
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.)