Saving a file in Mongodb's GridFS with pymongo results in a truncated file - python 2.7 on Windows 7 Saving a file in Mongodb's GridFS with pymongo results in a truncated file - python 2.7 on Windows 7 mongodb mongodb

Saving a file in Mongodb's GridFS with pymongo results in a truncated file - python 2.7 on Windows 7


Heh, changing

fileID = fs.put( open( r'owl.jpg', 'r')  )

to:

fileID = fs.put( open( r'owl.jpg', 'rb')  )

Fixes the behavior of the program on Windows 7. Too bad the behavior is different between OS's...


you already got the answer, but for the curious:

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files.