[New-bugs-announce] [issue44055] NamedTemporaryFile opened twice on Windows

Lumír Balhar report at bugs.python.org
Thu May 6 06:47:41 EDT 2021

New submission from Lumír Balhar <frenzy.madness at gmail.com>:


The documentation about tempfile.NamedTemporaryFile[0] contains:

That name can be retrieved from the name attribute of the returned file-like object. 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).

But after some testing on Windows, it seems that there is no problem in opening a temporary file for the second time. A simple reproducer:

from tempfile import NamedTemporaryFile
# Open the file for the first time
tmp_file = NamedTemporaryFile(mode="w+", delete=False)
content_original = tmp_file.read()
print("Original content:", content_original)
# Open the file for the second time
with open(tmp_file.name, mode="w+") as file:
    content = file.read()
    print("Updated content:", content)

The output is:

Original content: foo
Updated content: bar

So the question is: do I misunderstand the documentation? Or is there any automatic magic handling this on Windows?

I'm not a windows user and I've found this accidentally when preparing a Windows CI job for a project.

[0] https://docs.python.org/3/library/tempfile.html?highlight=namedtemporaryfile#tempfile.NamedTemporaryFile

assignee: docs at python
components: Documentation, Windows
messages: 393082
nosy: docs at python, frenzy, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: NamedTemporaryFile opened twice on Windows
versions: Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list