Python Temporary Files and Directories: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=External= * https://docs.python.org/3/library/tempfile.html =Internal= * File Operations in Python Create a...")
 
Line 4: Line 4:
* [[File_Operations_in_Python#Temporary_Files_and_Directories|File Operations in Python]]
* [[File_Operations_in_Python#Temporary_Files_and_Directories|File Operations in Python]]


=Auto-Deleting Temporary Files and Directories=


=Temporary Files and Directories to be Deleted by the Caller=





Revision as of 20:08, 13 June 2023

External

Internal

Auto-Deleting Temporary Files and Directories

Temporary Files and Directories to be Deleted by the Caller

Create a named temporary file, write in it and return the name. The file is not automatically deleted after close(), the caller will need to delete it:

import tempfile
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as t:
  t.write(something)
  return t.name