Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Make multiple directories based on a List using Python
Next article icon

Create temporary files and directories using tempfile

Last Updated : 18 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python tempfile module allows you to create a temporary file and perform various operations on it. Temporary files may be required when we need to store data temporarily during the program’s execution or when we are working with a large amount of data. These files are created with unique names and stored in a platform-dependent default location. The files created using tempfile module are deleted as soon as they are closed. 

In this tutorial, we will cover how to create and edit temporary files:

Creating a Temporary File

The file is created using the TemporaryFile() function of the tempfile module. By default, the file is opened in w+b mode, that is, we can both read and write to the open file. Binary mode is used so that files can work with all types of data. This file may not have a proper visible name in the file system.

Example:

Python3




import tempfile
 
temp = tempfile.TemporaryFile()
print(temp)
print(temp.name)
 
 

Output:

<_io.BufferedRandom name=7>
7

The function returns a file-like object that can be used as a temporary storage area. name attribute is used to get the random and unique name of the file. 

Note: This is not an actual visible filename and there is no reference to this file in the file system.

Creating a Named Temporary File

The NamedTemporaryFile() function creates a file in the same way as TemporaryFile() but with a visible name in the file system. It takes a delete parameter which we can set as False to prevent the file from being deleted when it is closed.

 Example:

Python3




import tempfile
 
temp = tempfile.NamedTemporaryFile()
print(temp)
print(temp.name)
 
 

Output:

<tempfile._TemporaryFileWrapper object at 0x7f77d332f6d8>
/tmp/tmprumbbjz4

This also returns a file-like object as before, the only difference is that the file has a visible name this time. 

Adding a Suffix and a Prefix to a Temporary File

We may choose to add a suffix or prefix to the name of a named temporary file, by specifying the parameters ‘suffix’ and ‘prefix’. 

Example 

Python3




import tempfile
 
temp = tempfile.NamedTemporaryFile(prefix='pre_', suffix='_suf')
print(temp.name)
 
 

Output:

/tmp/pre_ddur6hvr_suf

Reading and Writing to a Temporary File

The write() method is used to write to a temporary file. It takes input as binary data by default. We can pass the string to be written as input, preceded by a ‘b’ to convert it to binary data. 

The write function returns the number of characters written. If we open the file in text mode(w+t), we can use the writelines() method instead, which takes a string parameter. After writing to the file, the pointer is at the end of the file. So, before we can read the contents, seek() method is called to set the file pointer at the start of the file. 

seek() takes as argument the index of the character before which we want to place the pointer. The read() function is then used to read the contents.

Example:

Python3




import tempfile
 
temp = tempfile.TemporaryFile()
temp.write(b'foo bar')
temp.seek(0)
print(temp.read())
 
temp.close()
 
 

Output :

b'foo bar'

Creating a Temporary Directory

Like creating files, we can also create a temporary directory to store our temporary files. The TemporaryDirectory() function is used to create the directory. After we are done working with the temporary files, the directory needs to be deleted manually using os.removedirs().

Example: 

Python3




import tempfile
import os
  
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir)
 
 

Output:

<TemporaryDirectory '/tmp/tmpgjl5ki_5'>

Secure Temporary File and Directory

We can securely create a temporary file using mkstemp(). The file created by this method is readable and writable only by the creating user. We can add prefix and suffix parameters like in NamedTemporaryFile(). The default mode is binary, but we can open it in text mode by setting the ‘text’ parameter as True. This file does not get deleted when closed.

 Example:

Python3




import tempfile
 
  
secure_temp = tempfile.mkstemp(prefix="pre_",suffix="_suf")
print(secure_temp)
 
 

Output: 

(71, '/tmp/pre_i5us4u9j_suf')

Similarly, we can create a secure temporary directory using mkdtemp() method.

Example:

Python3




import tempfile
  
secure_temp_dir = tempfile.mkdtemp(prefix="pre_",suffix="_suf")
print(secure_temp_dir)
 
 

Output:

/tmp/pre_9xmtwh4u_suf

Location of Temporary Files

We can set the location where the files are stored by setting the tempdir attribute. The location can be fetched using gettempdir() method. When we create a temporary file or directory, Python searches in a standard list of directories to find one in which the calling user can create files. 

The list in order of preference is :

  1. The directory named by the TMPDIR environment variable.
  2. The directory named by the TEMP environment variable.
  3. The directory named by the TMP environment variable.
  4. A platform-specific directory:
    • 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.
  5. The current working directory.

Example:

Python3




import tempfile
 
tempfile.tempdir = "/temp"
print(tempfile.gettempdir())
 
 

Output:

/temp

We have covered the methods used to create temporary files and directories. We have also covered different operations like creating named temporary files, adding prefixes and suffixes, and setting the location of the temporary files.

Temporary files are a very important concept in Advanced Python programming. Creating and using temporary files will help you in many operations like handling intermediate data, testing, development, etc.



Next Article
Make multiple directories based on a List using Python

C

cosine1509
Improve
Article Tags :
  • Python
  • python-modules
  • python-utility
Practice Tags :
  • python

Similar Reads

  • Make multiple directories based on a List using Python
    In this article, we are going to learn how to make directories based on a list using Python. Python has a Python module named os which forms the core part of the Python ecosystem. The os module helps us to work with operating system folders and other related functionalities. Although we can make fol
    3 min read
  • Create an empty file using Python
    File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with
    3 min read
  • How to Create Directory If it Does Not Exist using Python?
    In this article, We will learn how to create a Directory if it Does Not Exist using Python. Method 1: Using os.path.exists() and os.makedirs() methods Under this method, we will use exists() method takes path of demo_folder as an argument and returns true if the directory exists and returns false if
    2 min read
  • How to create a duplicate file of an existing file using Python?
    In this article, we will discuss how to create a duplicate of the existing file in Python. Below are the source and destination folders, before creating the duplicate file in the destination folder. After a duplicate file has been created in the destination folder, it looks like the image below. For
    5 min read
  • Listing out directories and files in Python
    The following is a list of some of the important methods/functions in Python with descriptions that you should know to understand this article. len() - It is used to count number of elements(items/characters) of iterables like list, tuple, string, dictionary etc. str() - It is used to transform data
    6 min read
  • Create a directory in Python
    In Python, you can create directories to store and manage your data efficiently. This capability is particularly useful when building applications that require dynamic file handling, such as web scrapers, data processing scripts, or any application that generates output files. Let's discuss differen
    4 min read
  • How to create a list of files, folders, and subfolders in Excel using Python ?
    In this article, we will learn How to create a list of Files, Folders, and Sub Folders and then export them to Excel using Python. We will create a list of names and paths using a few folder traversing methods explained below and store them in an Excel sheet by either using openpyxl or pandas module
    12 min read
  • Python - List Files in a Directory
    Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python. Table of Content What is a Directory in Python?How to List Files in a Directory in Python
    9 min read
  • Python - Read file from sibling directory
    In this article, we will discuss the method to read files from the sibling directory in Python. First, create two folders in a root folder, and one folder will contain the python file and the other will contain the file which is to be read. Below is the dictionary tree: Directory Tree: root : | |__S
    3 min read
  • Change current working directory with Python
    The OS module in Python is used for interacting with the operating system. This module comes under Python's standard utility module so there is no need to install it externally. All functions in OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments t
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences