gpt4 book ai didi

python - 使用 pymongo 在 Mongodb 的 GridFS 中保存文件会导致文件被截断 - Windows 7 上的 python 2.7

转载 作者:IT老高 更新时间:2023-10-28 13:20:15 24 4
gpt4 key购买 nike

使用 pymongo 在 Mongodb 的 GridFS 中保存文件会导致文件被截断。

from pymongo import MongoClient
import gridfs
import os

#just to make sure we aren't crazy, check the filesize on disk:
print os.path.getsize( r'owl.jpg' )

#add the file to GridFS, per the pymongo documentation: http://api.mongodb.org/python/current/examples/gridfs.html
db = MongoClient().myDB
fs = gridfs.GridFS( db )
fileID = fs.put( open( r'owl.jpg', 'r') )
out = fs.get(fileID)
print out.length

在 Windows 7 上,运行此程序会生成以下输出:

145047
864

在 Ubuntu 上,运行这个程序会生成这个(正确的)输出:

145047
145047

不幸的是,我正在开发的应用程序针对的是 Windows 操作系统...

任何帮助将不胜感激!

所以你可以更严格地复制我的例子,'owl.jpg' 是从以下位置下载的:http://getintobirds.audubon.org/sites/default/files/photos/wildlife_barn_owl.jpg

最佳答案

呵呵,变态

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

到:

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

修复了程序在 Windows 7 上的行为。太糟糕了,操作系统之间的行为不同......

关于python - 使用 pymongo 在 Mongodb 的 GridFS 中保存文件会导致文件被截断 - Windows 7 上的 python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20551602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com