Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create Temporary Files and Directories in Python

  • 15 Apr, 2025
  • Com 0
How to Create Temporary files and Folders in Python

Python has a built-in module called “tempfile” that provides the .TemporaryFile() and .TemporaryNamedFile() methods that will create a temporary file (without or with name) efficiently.

Creating a temp files and directories in Python using tempfile module

Why do we need to use temporary folders and files? While executing a program, numerous things happen behind the scenes, and you need to store data temporarily. For that, we need to create a temporary file, which must be in a secure location and either unnamed or have an unpredictable name.

The tempfile.Temporary() function creates a file-like object that stores data without requiring a file system.

Always use the context manager like “with statement” for automatic cleanup. Always remember to close the tempfile unless you need to keep it open. It prevents orphaned files or directories from cluttering the system.

The permission of the temporary file is always 0o600, which allows you to read and write.

Unnamed temp file

If you want a temp file for a shorter amount of time and it is not necessary to have a name, you can use the tempfile.Temporary() method.

Before going further, you need to import tempfile at the head of your file. Then, we can use its methods.

import tempfile

with tempfile.TemporaryFile() as temp_file:
    temp_file.write(b"Hello mate, from temp file!")
    temp_file.seek(0)
    content = temp_file.read()
    print(content.decode())

# Output: Hello mate, from temp file!

Even though it is a temp file with no name, it will still occupy space in memory.

On macOS, I can see the tempfiles like this:

tempfiles on mac

It temporarily stores in a complex location like this: /var/folders/8k/x52znv0n7r53eebnmngj4kmw0400gn/T

The file has no visible names, so it is safe for sensitive data.

If you are working with very large datasets, always use the TemporaryFile() method. It uses disk storage but is optimized for temporary data that does not persist.

Named temp file

The tempfile.NamedTemporaryFile() method creates a named file. A named temp file is helpful when another process wants to use it.

from tempfile import NamedTemporaryFile

with NamedTemporaryFile(mode='w+', delete=True, suffix='.log') as tmp:
    print(f"Temporary file path: {tmp.name}")
    # File is deleted when `delete=True` (default) after context exits.

# Output: Temporary file path: /var/folders/8k/x42znv0n7r5383bnmngk4kmw0900gn/T/tmpuvh9_37x.log

By default, when the “with” context is complete, the file will be automatically deleted. If you pass delete=False, you need to clear the temp file manually.

To customize the file name, you can use the “suffix/prefix” argument.

The above code will work fine on macOS or Linux, but it may have issues with Windows OS.

On Windows, when you create a temporary file using the above approach, the OS automatically locks it. To handle this, you need to pass delete=false and then manually delete the file using the OS.remove() method.

import tempfile
import os

with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as named_temp:
    named_temp.write(b"Temporary file with a name!")
    print("File name:", named_temp.name)

# Deleting the file manually if delete=False
os.remove(named_temp.name)

# Output: your windows path

Define a custom temp directory

You can use the “dir” parameter in the NamedTemporaryFile() method to specify a custom base directory.

NamedTemporaryFile(dir='/var/folders/8k/tmp')

Using SpooledTemporaryFile()

There is also another function from the tempfile module, which is SpooledTemporaryFile(). It optimizes small file handling by spooling data in memory.

from tempfile import SpooledTemporaryFile

with SpooledTemporaryFile(max_size=1024) as tmp:
    tmp.write(b'Small data')  # Stays in memory
    tmp.rollover()  # Force write to disk
    print(tmp)
    
# Output: <tempfile.SpooledTemporaryFile object at 0x102a2fb50>

Using mkstemp()

If you want lower-level control over temporary files, use the tempfile.mkstemp() method. It returns a tuple containing an OS-level file descriptor and the path to the temporary file.

import os
from tempfile import mkstemp

# Create a temporary file
fd, path = mkstemp()
try:
    with os.fdopen(fd, 'w+') as tmp:
        tmp.write('Manual cleanup example')
        print(path)
finally:
    os.remove(path)  # Manual cleanup
    
# Output: /var/folders/8k/x42znv0n7r5383bnnmgl4kmw0009gn/T/tmp7kcdwijz

Your temp file path will be different from mine, and it depends on the system’s configuration.

Creating a temporary directory

In Python3, you can use the tempfile.TemporaryDirectory() method to create a temporary directory. To automatically delete it, we should combine this function with the “with” context manager.

from tempfile import TemporaryDirectory

with TemporaryDirectory() as dirpath:
    print(f"Temporary directory created at: {dirpath}")
    # Work with files inside the directory here
    with open(f"{dirpath}/temp_file.txt", "w") as f:
        f.write("Hello from the temp directory!")
# Directory and all contents are deleted here

# Output: Temporary directory created at:
# /var/folders/8l/x42qnv0n7r5383bnmngj5kmw0000gm/T/tmp21xelljj

The above code will generate a temp folder, and when the “with” block exists, this folder will be deleted automatically.

You can control the output temp directory’s name using suffix, prefix, and dir arguments like this:

from tempfile import TemporaryDirectory

with TemporaryDirectory(
    prefix="mydata_", 
    suffix="_tmp", 
    dir="/custom/path"
) as dirpath:
    print(f"Custom directory: {dirpath}")  
    
# Output: Custom directory: /custom/path/mydata_a8b3kfp2_tmp

If automatic cleanup fails somehow, you can use the .cleanup() method.

from tempfile import TemporaryDirectory

temp_dir = TemporaryDirectory()

dirpath = temp_dir.name

print(f"Directory: {dirpath}")

# Manually deleting the directory
temp_dir.cleanup()
# Directory is now gone

# Output: Directory: /var/folders/8k/x42zmn0n7r5383bnmnkb4kmw0000kl/T/tmpphe2bb4b

Using mkdtemp()

For getting lower-level control, you can use the tempfile.mkdtemp() method. It creates a temporary directory and returns its path.

from tempfile import mkdtemp
import os

# Create a temporary directory using mkdtemp()
dir_path = mkdtemp()
try:
    print(f"Directory: {dir_path}")
finally:
    os.rmdir(dir_path) 
    
# Output: Directory: /var/folders/8m/x21znv0n7r5383bnmngj4kmw0000kb/T/tmpx0c8i6bc

That’s all!

Post Views: 346
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Create Directory If It Does Not Exist in Python
How to Handle JSON NULL in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend