From the course: Python Standard Library Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating temporary files and directories

Creating temporary files and directories - Python Tutorial

From the course: Python Standard Library Essential Training

Start my 1-month free trial

Creating temporary files and directories

- [Instructor] Sometimes your programs will need to create files or directories that are only needed temporarily. The standard library contains a module specifically for working with these kinds of files and directories. So here in my temp file start file I've imported the temp file module along with the OS module for some utility functions I'll need later. The temp file module gives us functions for working with temporary data, and also allows us to examine properties related to these files. So I can use the gettempdir and gettempprefix functions to see which directory files will be saved to, and what file prefix they'll be given. So let's print that out. And let's do gettempprefix as well. There we go. So to actually create a temporary file using the most secure method that the underlying OS has available I can use the make s temp function. This function returns a tuple containing an OS level file handle and the absolute path name to that file. So I'm going to write temp file handle…

Contents