Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick way to save a python TempFile?

I am using tempfile.TemporaryFile for some intermediate processing. The program nicely removes the temp file for me when it closes, which is precisely the behaviour that I want the majority of the time. But is there a way to save the file for examination in the event of a (trapped) error or other abnormal circumstance?

like image 969
TimothyAWiseman Avatar asked Apr 12 '12 17:04

TimothyAWiseman


People also ask

Where is Tempfile stored python?

The directory named by the TEMP environment variable. The directory named by the TMP environment variable. A platform-specific location: On Windows, the directories C:\TEMP , C:\TMP , \TEMP , and \TMP , in that order.

How do I write a Tempfile?

If you want to write text data into a temp file, you can use the writelines() method instead. For using this method, we need to create the tempfile using w+t mode instead of the default w+b mode. To do this, a mode param can be passed to TemporaryFile() to change the mode of the created temp file.

How do I create a temp folder in Python?

import tempfile # create a temporary directory using the context manager with tempfile. TemporaryDirectory() as tmpdirname: print('created temporary directory', tmpdirname) # Outside the context manager, directory and contents have been removed.


1 Answers

Seek to the beginning and use shutil.copyfileobj() to copy the data to a file you create.

like image 136
Ignacio Vazquez-Abrams Avatar answered Oct 27 '22 13:10

Ignacio Vazquez-Abrams