Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a temporary file

Tags:

python

xlwt

I'm using xlwt in python to create a Excel spreadsheet. You could interchange this for almost anything else that generates a file; it's what I want to do with the file that's important.

from xlwt import *

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

I want to I have two use cases for the file: I stream it out to the user's browser or I attach it to an email. In both cases the file only needs to exist the duration of the web request that generates it.

What I'm getting at, the reason for starting this thread is saving to a real file on the filesystem has its own hurdles (stopping overwriting, cleaning up the file once done). Is there somewhere I could "save" it where it lives only in memory and only for the duration of the request?

like image 646
Oli Avatar asked Aug 04 '10 13:08

Oli


People also ask

Can you save temporary files?

Keeping or Deleting Temp Files: What You Should Know Temp files aren't useless files; they contain data that can improve the performance of programs. If you customize the settings of a program, for instance, these changes may be saved in a temp file.

Where are temp files saved?

Temp files often have the extension . TMP and are stored in the C:\Users\AppData\Local\Temp folder. If you're working on a document, your word-processing app may create a temporary file to track your progress. If the app crashes, you'll be able to recover your data from the temp file.

How do I access my temporary files?

On your keyboard, press the Windows + R keys at the same time. In the Open field, type %temp%, then press ENTER. The temp folder will open. You can also access it on your Windows 10 PC via the shortcut button below, then choose Temporary files.

Do temporary files do anything?

Yes. Temporary files are meant to store information temporarily and don't rely on the information stored in the file. However, deleting a temporary file that is in use may cause errors with the program. To help prevent problems, many programs lock the file while in use to prevent it from being deleted.


1 Answers

cStringIO

(or mmap if it should be mutable)

like image 191
Katriel Avatar answered Oct 07 '22 02:10

Katriel