"That name can be retrieved from the name member of the file object."

means that you can get the name of the temporary file created like so:

In [4]: import tempfile

In [5]: tf = tempfile.NamedTemporaryFile()  
In [6]: tf.name  # retrieve the name of the temp file just created
Out[6]: 'c:\\blabla\\locals~1\\temp\\tmptecp3i'

Note: By default the file will be deleted when it is closed. However, if the delete parameter is False, the file is not automatically deleted. See the Python docs on this for more information.

Since you can retrieve the name of each of the 50 temp files you want to create, you can save them, e.g., in a list, before you use them again later (as you say). Just be sure to set the delete value accordingly so that the files don't disappear when you close them (in case you plan to close, and then later reopen them).

I explained how to create temporary filenames in more detail here Best way to generate random file names in Python

Answer from Levon on Stack Overflow
🌐
Python
docs.python.org › 3 › library › tempfile.html
tempfile — Generate temporary files and directories
Source code: Lib/tempfile.py This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFil...
Top answer
1 of 3
267

I think you're looking for a tempfile.NamedTemporaryFile.

import tempfile
with tempfile.NamedTemporaryFile() as tmp:
    print(tmp.name)
    tmp.write(...)

But:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

If that is a concern for you:

import os, tempfile
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
    print(tmp.name)
    tmp.write(...)
finally:
    tmp.close()
    os.unlink(tmp.name)
2 of 3
107

There is a tempfile module for python, but a simple file creation also does the trick:

new_file = open("path/to/FILE_NAME.ext", "w")

Now you can write to it using the write method:

new_file.write('this is some content')

With the tempfile module this might look like this:

import tempfile

new_file, filename = tempfile.mkstemp()

print(filename)

os.write(new_file, "this is some content")
os.close(new_file)

With mkstemp you are responsible for deleting the file after you are done with it. With other arguments, you can influence the directory and name of the file.


UPDATE

As rightfully pointed out by Emmet Speer, there are security considerations when using mkstemp, as the client code is responsible for closing/cleaning up the created file. A better way to handle it is the following snippet (as taken from the link):

import os
import tempfile

fd, path = tempfile.mkstemp()
try:
    with os.fdopen(fd, 'w') as tmp:
        # do stuff with temp file
        tmp.write('stuff')
finally:
    os.remove(path)

The os.fdopen wraps the file descriptor in a Python file object, that closes automatically when the with exits. The call to os.remove deletes the file when no longer needed.

🌐
W3Schools
w3schools.com › python › ref_module_tempfile.asp
Python tempfile Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training · ❮ Standard Library Modules · Create a temporary file and directory: import tempfile with tempfile.TemporaryFile(mode='w+') as f: f.write('Hello from Emil') f.seek(0) print(f'Content: {f.read()}') Try it Yourself » ·
🌐
Read the Docs
python.readthedocs.io › fr › stable › library › tempfile.html
11.6. tempfile — Generate temporary files and directories — documentation Python 3.6.1
>>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context ...
🌐
Heitor's Log
heitorpb.github.io › bla › pytmp
Temporary files and directories in Python | Heitor's log
Creating temporary files and directories in Python, using the standard library module `tempfile`.
Find elsewhere
🌐
Real Python
realpython.com › ref › stdlib › tempfile
tempfile | Python Standard Library – Real Python
The Python tempfile module provides a simple way to create temporary files and directories.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › tempfile.py
cpython/Lib/tempfile.py at main · python/cpython
"tempfile._TemporaryFileWrapper", message=( "{name!r} is deprecated and slated for removal in Python {remove}. " "Use tempfile.TemporaryFileWrapper instead." ), remove=(3, 21), ) return TemporaryFileWrapper ·
Author   python
🌐
Lbl
davis.lbl.gov › Manuals › PYTHON › library › tempfile.html
10.6. tempfile — Generate temporary files and directories — Python v2.6.6 documentation
August 24, 2010 - tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]])¶
🌐
Python.org
discuss.python.org › ideas
Create temporary files in a temporary directory - Ideas - Discussions on Python.org
January 16, 2024 - Often, I find myself needing to create multiple temporary files that are related. It would be nice if tempfile.TemporaryDirectory could make that easier for me using something like this: with tempfile.TemporaryDirectory() as temp_dir: fd1, temp_file1_path = temp_dir.mkstemp() fd2, temp_file2_path = temp_dir.mkstemp() # Do something with the files...
🌐
Python.org
discuss.python.org › ideas
tempfile.TemporaryDirectory().name should return pathlib.Path instead of str
December 11, 2023 - if not that then maybe another property instead? Something like >>> tempfile.TemporaryDirectory().name 'C:\\Users\\monarch\\AppData\\Local\\Temp\\tmp57z91nmz' >>> tempfile.TemporaryDirectory().path WindowsPath('C:/Users…
🌐
Safjan
safjan.com › home › note › mastering temporary files and directories with...
Mastering Temporary Files and Directories with Python's tempfile Module
July 12, 2023 - Python's tempfile module is an incredibly powerful tool that allows you to create and manage temporary files and directories with ease.
🌐
TakoVibe
takovibe.com › home › blog › python tempfile explained: temporary files, auto-deletion & common mistakes
Python tempfile Explained: Temporary Files, Auto-Deletion & Common Mistakes | Rahul Beniwal | TakoVibe
August 21, 2025 - tempfile is a built-in Python module that creates temporary files and directories which are automatically deleted when they are no longer needed.
🌐
Reddit
reddit.com › r/pythontips › how to use tempfile to create temporary files and directories in python
r/pythontips on Reddit: How To Use tempfile To Create Temporary Files and Directories in Python
March 12, 2023 -

Python has a rich collection of standard libraries to carry out various tasks. In Python, there is a module called tempfile that allows us to create and manipulate temporary files and directories.

We can use tempfile to create temporary files and directories for storing temporary data during the program execution. The module has functions that allow us to create, read, and manipulate temporary files and directories.

The tempfile module includes a function called TemporaryFile() that allows us to create a temporary file for use as temporary storage.

Here the guide to generate and manipulate the temporary files using the tempfile module👇👇

Generate And Manipulate Temporary Files and Directories in Python

🌐
Medium
medium.com › @pijpijani › efficient-management-of-temporary-files-with-namedtemporaryfile-in-python-b8d48cbcab45
Efficient Management of Temporary Files with NamedTemporaryFile in Python | by Pikho | Medium
March 12, 2023 - NamedTemporaryFile is a Python function that creates a temporary file with a unique name in a specified directory, which can be used for storing temporary data. When you use NamedTemporaryFile, a file object is returned, which can be used to ...
🌐
Readthedocs
nxpy.readthedocs.io › en › latest › temp_file.html
temp_file - Temporary files that support the context protocol — Nxpy 1.0.5 documentation
Requires at least Python 2.6 · class TempDir(*args, **kwargs)[source]¶ · A temporary directory that implements the context manager protocol. The directory is removed when the context is exited from. Uses tempfile.mkdtemp() to create the actual directory. __init__(*args, **kwargs)[source]¶ ·
🌐
Nousresearch
hermes-agent.nousresearch.com › persistent memory
Persistent Memory | Hermes Agent
Easily re-discovered facts: "Python 3.12 supports f-string nesting" — can web search this
🌐
Python-Fiddle
python-fiddle.com
Python Fiddle: Online Python IDE, Compiler, and Interpreter
Run Python code in your browser. Share code snippets with others.
🌐
Conda
docs.conda.io › projects › conda › en › stable › user-guide › tasks › manage-environments.html
Managing environments — conda 26.5.3 documentation
The file above defines an environment named, "myenv" that uses conda-forge as its channel and adds the dependencies python and numpy.