I have an object (called "img") of the werkzeug.datastructures.FileStorage
class (this object represents a file). I need to save this file on the disk. I can do it in the following way:
img.save(fname)
It works fine. But before I save the file, I need to check its size. I do it in the following way:
img.seek(0, os.SEEK_END)
size = img.tell()
It works also fine. But the problem is that I cannot save the file after I check its size. Or, more precisely, I get a file on the disk but it is empty, if I checked its size before.
How can I check the size of the file without "destroying" it?
you forgot to seek the file to its beginning before saving it, hence the empty file
#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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With