Skip to content

Temporary File/Directory

Overview

Usecase

  • Download resource in temporary folder

How it work

It depend on

Linux | Window

-> C://temp

/tmp/...

C:///temp/

The order of search:

Python searches a standard list of directories to find one which the calling user can create files in. The list is:

The directory named by the TMPDIR environment variable.

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.

On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.

As a last resort, the current working directory.

Using tempfile to temporary download file

The temporary is very useful to be an transition place when download file

Compare with the general way

Type General Using tempfile
Manage directory Required code to establish the folder No,

tempfile.gettempdir()

Return the name of the directory used for temporary files. This defines the default value for the dir argument to all functions in this module.

Python searches a standard list of directories to find one which the calling user can create files in. The list is:

The directory named by the TMPDIR environment variable.

The directory named by the TEMP environment variable.

The directory named by the TMP environment variable.

import tempfile

# Get tempfile
tempfile.gettempdir()
# >>> 'C:\\Users\\<user-name>\\\\AppData\\Local\\Temp'

A platform-specific location:

On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.

On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.

As a last resort, the current working directory.

The result of this search is cached, see the description of tempdir below.

Other alternative

Example usecase

Reference