- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate temporary files and directories using Python
The tempfile module in standard library defines functions for creating temporary files and directories. They are created in special temp directories that are defined by operating system file systems. For example, under Windows the temp folder resides in profile/AppData/Local/Temp while in linux the temporary files are held in /tmp directory.
Following functions are defined in tempfile module
This function creates a temporary file in the temp directory and returns a file object, similar to built-in open() function. The file is opened in wb+ mode by default, which means it can be simultaneously used to read/write binary data in it. What is important, the file’s entry in temp folder is removed as soon as the file object is closed. Following code shows usage of TemporaryFile() function.
>>> import tempfile >>> f = tempfile.TemporaryFile() >>> f.write(b'Welcome to TutorialsPoint') >>> import os >>> f.seek(os.SEEK_SET) >>> f.read() b'Welcome to TutorialsPoint' >>> f.close()
Following example opens TemporaryFile object in w+ mode to write and then read text data instead of binary data.
>>> ff = tempfile.TemporaryFile(mode = 'w+') >>> ff.write('hello world') >>> ff.seek(0) >>> ff.read() 'hello world' >>> ff.close()
This function is similar to TemporaryFile() function. The only difference is that a file with a random filename is visible in the designated temp folder of operating system. The name can be retrieved by name attribute of file object. This file too is deleted immediately upon closing it.
>>> fo = tempfile.NamedTemporaryFile() >>> fo.name 'C:\\Users\\acer\\AppData\\Local\\Temp\\tmpipreok8q' >>> fo.close()
This function creates a temporary directory. You can choose the location of this temporary directory by mentioning dir parameter. Following statement will create a temporary directory in C:\\python36 folder.
>>> f = tempfile.TemporaryDirectory(dir = "C:/python36") <TemporaryDirectory 'C:/python36\\ tmp9wrjtxc_'>
The created directory appears in the dir1 folder. It is removed by calling cleanup() function on directory object.
>>> f.name 'C:/python36\\tmp9wrjtxc_' >>> f.cleanup()
This unction also creates a temporary file, similar to TemporaryFile() function. Additionally, suffix and prefix parameters are available to add with temporary file created. Unlike in case of TemporaryFile(), the created file is not automatically removed. It should be removed manually.
>>> f = tempfile.mkstemp(suffix = '.tp') C:\Users\acer\AppData\Local\Temp\tmpbljk6ku8.tp
This function also creates a temporary directory in operating system’s temp folder and returns its absolute path name. To explicitly define location of its creation, use dir parameter. This folder too is not automatically removed.
>>> tempfile.mkdtemp(dir = "c:/python36") 'c:/python36\\tmpruxmm66u'
This function returns name of directory to store temporary files. This name is generally obtained from tempdir environment variable. On Windows platform, it is generally either user/AppData/Local/Temp or windowsdir/temp or systemdrive/temp. On linux it normally is /tmp. This directory is used as default value of dir parameter.
>>> tempfile.gettempdir() 'C:\\Users\\acer\\AppData\\Local\\Temp'
In this article functions in tempfile module have been explained.
- Listing out directories and files using C#
- Listing out directories and files in Python?
- How to list non-hidden files and directories in windows using Python?
- How to generate secure temporary file name using Python?
- Temporary files in Java
- Generate and parse Mac OS X .plist files using Python (plistlib)
- How to Protect Files and Directories from Deleting in Linux
- Find the Largest Top 10 Files and Directories On a Linux
- How to remove files and directories in the Linux operating system using the terminal?\n
- Directories in Python
- C Program to list all files and sub-directories in a directory
- How to move a file, group of files, and directories in Linux?
- Encode and decode uuencode files using Python
- How to compare the files available in two directories using diff command in Linux?
- How to Find a Specific String or Word in Files and Directories in Linux