Get Temp Directory in Python

Sometimes we can encounter scenarios where we wish to store temporary data in Python. This can include situations of creating temporary files and directories.

In this tutorial, we will discuss how to get temp directory in Python.

Get Temp Directory in Python

The tempfile module is used in Python to work with and create temporary files and directories. We will discuss different methods from this module to get temp directory in Python.

Using the tempfile.Gettempdir() Function to Get Temp Directory in Python

The gettempdir() function from the tempfile module is used to return the default temporary directory that is used by the user for working with temporary files. It follows a set of precedents to determine the temporary directory.

This directory can be interpreted from the environment variables. Usually, it is the directory named TMP, TEMP, or TMPDIR by the environment variable.

If the directory cannot be interpreted from the same then it may return the platform-specific directory. This can be like C:\TMP, C:\TEMP in the windows or like tmp, usr/tmp on other platforms.

If still the temporary directory is not determined, then the function will return the current working directory as the same.

For example,

Output:

C:\tmp

The above example shows how to get temp directory in Python using the gettempdir() method. In case, you need to create temp file, you can follow how to create temp file in Python.

In Python 3.1 and above, the result is returned as a string. To get the value in bytes we can use the function discussed below.

Using the tempfile.Gettempdirb() Function to Get Temp Directory in Python

The gettempdirb() function from the tempfile module returns the temporary directory as bytes. That is the only distinction between the gettempdir() and gettempdirb() functions.

This can be useful in Python 3 which changed the encoding and introduced a distinction between strings and bytes.

For example,

Output:

b’C:\tmp’

The b prefix in the above example indicates that the final result is in the form of bytes.

Using the tempfile.Tempdir Variable to Get Temp Directory in Python

An important thing to remember is that both the above-mentioned methods use a pre-defined global variable to return the temp directory. This variable is called tempfile.tempdir. It is not recommended to set this variable manually and it is also not advised to set its value as bytes as it will alter the return type of functions like mkdtemp and mkstemp that create further temp directories and files.

We can view this variable directly as well after calling any of the above two functions.

For example,

Output:

C:\tmp

Conclusion

To conclude, we discussed several methods of the tempfile module to get temp directory in Python. First, we discussed the use of the tempfile module. Then, we demonstrated the use of the gettempdir() and gettempdirb() functions to get temp directory in Python in different formats. We discussed the precedence of rules to determine the same and how they both use the global variable tempdir to return the value.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *