Saving a temporary file

I use xlwtin python to create an Excel spreadsheet. You can exchange this for almost anything that creates a file; this is what i want to do with the important file.

from xlwt import *

w = Workbook()
#... do something
w.save('filename.xls')

I want to have two options for using the file: I transfer it to the user's browser or attach it to an email. In both cases, the file should only contain the duration of the web request that generates it.

What I get, the reason for starting this thread - saving the real file in the file system, has its own obstacles (stop overwriting, cleaning the file after it is executed). Somewhere I could "save" him where he lives only in memory and only at the time of the request?

+5
source share
2 answers

cStringIO

(or mmapif it needs to be changed)

+5
source

Summarizing the answer, as you suggested: if "anything else that generates the file" will not accept the file object, as well as the path to the file, then you can reduce the problem by using tempfile.NamedTemporaryFile

+1
source

All Articles