2024-05-15 20:38:18 发布
网友
我有一个werkzeug.datastructures.FileStorage类的对象(称为“img”)(该对象表示一个文件)。我需要把这个文件保存在磁盘上。我可以用以下方式来做:
werkzeug.datastructures.FileStorage
img.save(fname)
它工作得很好。但是在我保存文件之前,我需要检查它的大小。我的方法如下:
它也很好用。但问题是,我在检查文件大小后无法保存它。或者,更准确地说,我在磁盘上得到了一个文件,但是如果我之前检查过它的大小,它是空的。在
如何在不“销毁”文件的情况下检查文件的大小?在
代替seek and tell替换为:
import os img.flush() size = os.fstat(img.fileno()).st_size
在保存文件之前,您忘了查找文件的开头,因此文件为空
#seek to the end of the file to tell its size img.seek(0, os.SEEK_END) size = img.tell() #seek to its beginning, so you might save it entirely img.seek(0) img.save(fname)
Werkzeug的文件存储有一个content_length属性:http://werkzeug.pocoo.org/docs/0.10/datastructures/#werkzeug.datastructures.FileStorage.content_length
content_length
img.content_length
代替seek and tell替换为:
在保存文件之前,您忘了查找文件的开头,因此文件为空
Werkzeug的文件存储有一个
content_length
属性:http://werkzeug.pocoo.org/docs/0.10/datastructures/#werkzeug.datastructures.FileStorage.content_length相关问题 更多 >
编程相关推荐