8

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?

djvg
  • 8,264
  • 3
  • 53
  • 83
TimothyAWiseman
  • 13,397
  • 11
  • 37
  • 47

2 Answers2

8

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

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
3

Use tempfile.mkstemp(). According to the documentation:

Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.

Read more here.

dhg
  • 51,689
  • 8
  • 120
  • 144