I’m trying to create a temporary file from an entry in my database. I have done a print(filedata['data']) and I can see that the data is being retrieved properly but even with the temp_h.flush() I never see any data saved to the file. When I go to retrieve it, the file is empty. Could someone please explain to me what’s causing this and if there’s a better way to do what I’m trying to do with PIL, I’d love to learn that too. I’m running this on a Linux system with an EXT3 File system
def main():
'''Main'''
for nohisto in thestash.stash.get_files():
fileid = str(nohisto['_id'])
filedata = openstash(fileid)
temp_h = tempfile.TemporaryFile(mode='r+')
temp_h.write(filedata['data'])
temp_h.flush()
time.sleep(3)
myhisto = Image.open(temp_h).histogram()
Try
mode='w+', then after writing,seek(0)to put the i/o pointer back to the file beginnning. You can also create in-memory file-like objects using the StringIO module.